Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Sort Custom Post Type by Custom Field

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #171537
    [email protected]
    Participant

    How can you sort a custom post type by a custom field in WordPress? I’m trying to figure out a way to echo a custom field called ‘State’ alphabetically and place the custom post type links called employees under each of their respect states.

    So it would look like this:

    Alabama
    Appleseed, Johnny
    Chamber, Pete

    Florida
    Miller, Sam
    Stark, Amy

    Virginia
    Crites, Sue
    Lake, Mary

    I know how to write the basic loop for a custom post type but I’m not sure how to sort under a custom field belonging to that post type alphabetically.

    <?php $query = new WP_query ( array( 'post_type'=> 'Donor', 'meta_key'=> 'email', 'orderby'=> 'meta_value', 'order' => 'DESC'));
      while ( $query -> have_posts() ) : $query -> the_post(); ?>
            <?php echo get_post_meta($post->ID, 'email', true); ?>
        &lt;h2&gt;<a>"&gt;&lt;?php the_title(); ?&gt;</a>&lt;/h2&gt;
      &lt;?php
    endwhile;   
    wp_reset_query();  ?&gt;
    
Viewing 1 post (of 1 total)
  • The forum ‘Back End’ is closed to new topics and replies.