I have a custom post type. I use ACF for the layout of this custom post type. In ACF, the field is called "temoignage_category", the field type is a "select" and in the "choices" field there are 4 different options. In the edit page of each custom post type, I select one of the 4 options

What I want is to display these 4 options as section titles, then under each of them display the corresponding custom post type

For example : My custom field value 1

  • post 1
  • post 2
  • post 3

My custom field Value 2

  • post 4
  • post 5
  • post 6


$temoignage_category = get_field('temoignage_category');
        $args = array(
                'posts_per_page'   => -1,
                'post_type'     => 'temoignage',
                'meta_key' => 'temoignage_category',
                'meta_value' => $temoignage_category,
                'orderby'           => 'meta_value'
          $the_query = new WP_Query( $args );
          echo "<div class='temoignagerow'>";
          if( $the_query->have_posts() ): 
            if( $temoignage_category ) { echo '<h2>'.$temoignage_category.'</h2>'; };
          while( $the_query->have_posts() ) : $the_query->the_post();

          $color = get_field('background_color', get_field('chapitre', $value->ID)->ID); ?>
          <a class="temoignagecard " style="border-color: <?= $color ?>; color: <?= $color ?>"
             href="<?= get_permalink($value->ID) ?>">
            <div class="inner archive_inner">
              <img src="<?= get_field("photo", $value->ID)['url'] ?>" alt="">
              <p><?= get_the_title($value->ID) ?></p>
      echo '</div>';

This loop just allows me to display the custom post types in order, but it does not display the custom field with the corresponding custom post types below.

I know you have to nest the loops, but all I can do for now is display the custom post type. could someone tell me how to proceed?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.