admin管理员组

文章数量:1289363

I have a post_type of 'interview' and within it is a repeater field 'interview' and sub_fields 'question' and 'answer'. I would like to display all questions and answers from all interviews and orderby question, perhaps alphabetically. So that on the actual page you see different answers from the same question.

Right now I am able to list all questions and answers but any attempt I make at orderby just orders them by question within the post and not over all.

What I want:

  • Q1 from Post 1
  • A1 from Post 1
  • Q1 from Post 2
  • A1 from Post 2
  • Q2 from Post 1
  • A2 from Post 1
  • Q2 from Post 2
  • A2 from Post 2

What I am getting:

  • Q1 from Post 1
  • A1 from Post 1
  • Q2 from Post 1
  • A2 from Post 1
  • Q1 from Post 2
  • A1 from Post 2
  • Q2 from Post 2
  • A2 from Post 2

I am using wp_query with args for post_type but it just seems that I'm actually returning just a list of all posts not a list of all custom fields because they stay grouped together by post.

 <?php 
    
    $args = array(
      'posts_per_page' => -1,
      'post_type' => 'interview',
      'orderby' => 'question',
      'order' => 'DESC'
  ); 

    $answer_query = new WP_Query($args);    ?>
  <?php
    while ($answer_query->have_posts()): $answer_query->the_post();
    if( have_rows('interview') ):
      while( have_rows ('interview') ): the_row();
        // Questions and Answers
          if( get_sub_field('question')){
            $question = get_sub_field('question');
          }
          if( get_sub_field('answer')){
            $answer = get_sub_field('answer');
          }
          if( get_sub_field('qaid')){
            $qaid = get_sub_field('qaid');
          }
    echo '<div class="question_container">';
    echo '<div class="question"><h3>' . $question . '</h3></div>';
    echo '<div class="answer">' . $answer . '</div>';
    echo '</div>';
    endwhile; 
    endif;
    endwhile;

    wp_reset_postdata();
    ?>

本文标签: wp queryHow can I display a custom field from all posts and order them individually from their parent post