Search code examples
wordpressadvanced-custom-fields

Get posts order by ACF field value as title in Wordpres


I have a custom post type: club-member
I would like to display a list of all members
and separate them by ACF value called: region
the region custom field is a text field with a dynamic value.

In the end, I should print something like this:

England (Dynamic "region" field within the post type "club-member")

  • Member 1
  • Member 2
  • Member 3
  • Member 4

USA (Dynamic "region" field within the post type "club-member")

  • Member 5
  • Member 6
  • Member 7

I am using a regular get_posts:

<?php
$args = array(
    'post_type'      => 'club-member',
    // 'order' => 'DESC',
    'posts_per_page' => -1,
);

$club_members = get_posts( $args );

if ( $club_members ) {
    foreach ( $club_members as $post ) {
        setup_postdata( $post );

        $post_title = $post->post_title;
        $region     = get_field( 'region' );
        // ACF

        ?>
        <h3><?php echo $post_title; ?></h3>
        <?php
    }
    wp_reset_postdata();
}
?>

In the end, I should get something like this:

<h1>Region (with associated members)</h1>
<h3>Club member title</h3>
<h3>Club member title</h3>
<h3>Club member title</h3>
<h3>Club member title</h3>

<h1>Region (with associated members)</h1>
<h3>Club member title</h3>
<h3>Club member title</h3>
<h3>Club member title</h3>
<h3>Club member title</h3>

Note: the value of the field "region" is dynamic and should be only printed once and grouped underneath all the posts sharing the same field value


Solution

  • check the documentation of ACF here, I added a new query on top to get all the regions which we loop through in the second query.

    <?php 
    
    // args
    $args = array(
        'numberposts'   => -1,
        'post_type'     => 'club-member',
    );
    $regions = array(); 
    $the_query = new WP_Query( $args ); 
    
     if( $the_query->have_posts() ): 
         while ( $the_query->have_posts() ) : $the_query->the_post(); 
            if(!in_array(get_field('region'), $regions)){
                array_push($regions, get_field('region'));
            }
         endwhile; 
        
     endif; 
    
     wp_reset_query(); 
    
    
    $query = array(
        'post_type' => 'club-member',
        'meta_query' => array(
            'relation' => 'OR'
        )
    );
    
    foreach($regions as $region) {
        array_push($query['meta_query'], array(
            'key' => 'region',
            'value' => $region,
            'compare' => 'LIKE'
        ));
    } 
       
    $the_query = new WP_Query( $args );
    
    ?>
    <?php if( $the_query->have_posts() ): ?>
    <ul>
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <?php $region =  get_field('region'); ?>
        <?php if (in_array($region, $regions)) { ?>
        <h1>
            <?php
                echo(get_field('region')); 
                $index = array_search($region, $regions);
                if($index !== FALSE){
                    unset($regions[$index]);
                }
            ?>
        </h1>
        <?php } ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>
        <?php endwhile; ?>
    </ul>
    <?php endif; ?>
    
    <?php wp_reset_query();  ?>