Support

Account

Home Forums General Issues Order cpt by custom field

Unread

Order cpt by custom field

  • I have been trying to figure this out for a while now. I create a custom post type for speakers and created custom fields for first name and last name. I want to order the posts alphabetically by the custom field last name. Currently it is just ordering it by post creation date. Here is what I have

    <?php 
    $speaker_list = new WP_Query(array(
        'post_type'		=> 'speakers',
        'posts_per_page'    => -1,
        'meta_key'		=> 'last_name', 
        'tax_query'         => array(
    	array(
    	  'taxonomy' 	=> 'speaker_type',
    	  'terms'    	=> 'faculty',
    	),
         ), 
        'orderby' 		=> 'meta_value', 
        'order' 		=> 'ASC',
    )); ?>
    
    <?php if ( $speaker_list->have_posts() ) : ?>
        <?php while ( $speaker_list->have_posts() ) : $speaker_list->the_post(); ?>
    
    		<!-- Displays the speaker info -->
    
        <?php endwhile; 
        wp_reset_postdata();
    else : ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>
Viewing 1 post (of 1 total)

The topic ‘Order cpt by custom field’ is closed to new replies.