I have a repeater field (speakers) which is set to show post objects (speaker). I would like these to show alphabetically based on the last_name field. I cannot figure out how to change the post object query to sort by a custom field.
<?php if(get_field('speakers')): ?>
<div id="event-speakers">
<?php while(has_sub_field('speakers')): $post_object = get_sub_field('speaker');
if( $post_object ):
$post = $post_object;
setup_postdata( $post );
?>
<h3><?php the_field('first_name');?> <?php the_field('last_name');?></h3>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
</div><!--/#event-speakers-->
<?php endif; ?>
Hi @chriswhiteley,
You can make use of the the WP_Query object to load all the posts ordered by a custom field in your case based on the last_name field:
The code will look something like this:
<?php
// query
$the_query = new WP_Query(array(
'post_type' => 'post_type_name',
'posts_per_page' => -1,
'meta_key' => 'last_name',
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
?>