[Solved] How can I display a custom field from all posts and order them individually from their parent post

David Weihs Asks: How can I display a custom field from all posts and order them individually from their parent post
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.

Code:
 <?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();
    ?>

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.