Support

Account

Home Forums Add-ons Repeater Field Turn repeater subfields into variables

Helping

Turn repeater subfields into variables

  • I have a select repeater field being dynamically populated with the categories of a custom taxonomy. I want to turn the subfields into variables so they can be used in a template query with a terms array.

    $the_query section of code below is working fine. What I want is to replace the terms array with variables from repeater subfields.

    The top section of the code gets me part of the way there. The problem is, when I echo the output, it shows the subfields category numbers (e.g., 16, 15, 7 ) and not the actual value of the field.

    How do I get these variables to output the actual term values?

    
    global $post;
    $row1 = get_field('homepage_sidebar_sponsor_logos' );
    $first_row = $row1[0];
    $first_row_name = $first_row['contestant_year_category' ];
    
    $row2 = get_field('homepage_sidebar_sponsor_logos' );
    $second_row = $row2[1];
    $second_row_name = $second_row['contestant_year_category' ];
    
    $row3 = get_field('homepage_sidebar_sponsor_logos' );
    $third_row = $row3[2];
    $third_row_name = $third_row['contestant_year_category' ];
    
    echo $first_row_name . ", " , $second_row_name . ", ", $third_row_name;
    
    $the_query = new WP_Query( array(
        'post_type' => 'contestants',
        'tax_query' => array(
        array(
          'taxonomy' => 'year_type',
          'field' => 'slug',
          'terms' => array( 'Category One', 'Category Two', 'Category Three' )
        )
      ))
    ) ;
    
  • I got it working. As what I had above was showing the taxonomy term IDs I found how to turn these IDs into text terms that could be used within $the_query $terms array. I’m not sure if this is the best way to solve the problem so if you know any way to streamline this code, I’d greatly appreciate knowing how.

    global $post;
    $row1 = get_field('homepage_sidebar_sponsor_logos' );
    $first_row = $row1[0];
    $first_row_id = $first_row['contestant_year_category' ];
    $first_row_term = get_term_by('id', absint( $first_row_id ), 'year_type');
    $first_row_name = $first_row_term->name;
    
    $row2 = get_field('homepage_sidebar_sponsor_logos' );
    $second_row = $row2[1];
    $second_row_id = $second_row['contestant_year_category' ];
    $second_row_term = get_term_by('id', absint( $second_row_id ), 'year_type');
    $second_row_name = $second_row_term->name;
    
    $row3 = get_field('homepage_sidebar_sponsor_logos' );
    $third_row = $row3[2];
    $third_row_id = $third_row['contestant_year_category' ];
    $third_row_term = get_term_by('id', absint( $third_row_id ), 'year_type');
    $third_row_name = $third_row_term->name;
    
    $the_query = new WP_Query( array(
        'post_type' => 'contestants',
        'tax_query' => array(
        array(
          'taxonomy' => 'year_type',
          'field' => 'slug',
          'terms' => array( $first_row_name, $second_row_name, $third_row_name )
        )
      ))
    ) ;
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Turn repeater subfields into variables’ is closed to new replies.