Advertisement
secumbu

WP Get ACF Repeaters by Value ... working

Jan 31st, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.30 KB | None | 0 0
  1. function exercise_shortcode( $atts ) { 
  2.     //shortcode atts
  3.     $atts = shortcode_atts( array(
  4.         'muskelgruppe' => 'rücken',
  5.     ), $atts, 'exercise' );
  6.    
  7.     //Get Muskelgruppe from shortcode atts
  8.     $muskelgruppe = $atts['muskelgruppe'];
  9.    
  10.     //Get only Posts with a Repeater
  11.     $exercises = get_posts(
  12.         array(
  13.             'post_type'      => 'post',
  14.             'posts_per_page' => 500,
  15.             'meta_query'     => array(
  16.                 'relation' => 'AND',
  17.                 array(
  18.                     'key'     => 'uebungen',
  19.                     'compare' => 'EXISTS',
  20.                 ),
  21.             ),
  22.         )
  23.     );
  24.    
  25.     $output = "";
  26.     //Loop
  27.     if ( $exercises ) {
  28.         foreach ( $exercises as $exercise ) {
  29.             setup_postdata( $exercise );
  30.             if( have_rows('uebungen',$exercise->ID )) {
  31.                 while( have_rows('uebungen',$exercise->ID) ) : the_row();
  32.                     if( $muskelgruppe == get_sub_field('uebung_gruppe')) {
  33.                         //Hier kannst du deine gewünschten Felder abrufen
  34.                         $exercise_title = get_sub_field('uebung_name');
  35.                        
  36.                         //Dem Output anfügen  
  37.                         $output .= '<div class="uebung">';
  38.                         $output .= '<p>';
  39.                         $output .= $exercise_title;
  40.                         $output .= '</p>';
  41.                         $output .= '</div>';
  42.                     }      
  43.                
  44.                 endwhile;
  45.             }
  46.         }
  47.         wp_reset_postdata();
  48.     }
  49.     return $output;
  50. }
  51.  
  52. add_shortcode( 'exercices', 'exercise_shortcode' );
  53.  
  54. // Use [exercices muskelgruppe="xyz"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement