[CustomPress] Sort posts by custom field

I have a custom post-type “person” that will have a custom field “city”. I am able to sort posts by title, but would also like to sort by “city”, so that for each “city” I could echo the custom field value as a sub-headline ( <h2> ) and then list all post with same “city” – then the next “city” as a sub-headline and the persons with same “city”. Also “city” should be alphabetized as well as the listing of “person”. So the output should be something like this:

<h2>City 1</h2>
- person1_1 (with city1)
- person1_2 (with city1)
<h2>City 2</h2>
- person 2_1 (with city2)
- person 2_2 (with city 2)

Could you provide me with an example of code I can use for this?

  • Dimitris Kalliris
    • Support Team Lead

    Hello there Georg,

    hope you’re doing well! :slight_smile:

    You should be able to do so using query arguments like this:

    $query = query_posts( array(
    'post_type' => 'person',
    'order' => 'DESC',
    'meta_key' => '_ct_text_5c40dd53ea847',
    'orderby' => 'meta_value'
    ) );

    Reference: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    The meta_key above is from a testing site of mine, you should replace with your own as it can be found in CustomPress here:

    [attachments are only viewable by logged-in members]

    Above snippet can be combined with the actual WP loop in a similar way like this:

    <?php
    $query = query_posts( array(
    'post_type' => 'person',
    'order' => 'DESC',
    'meta_key' => '_ct_text_5c40dd53ea847',
    'orderby' => 'meta_value'
    ) );

    if (have_posts()) : while (have_posts()) : the_post(); ?>

    <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <?php the_content(); ?>
    </div>

    <?php endwhile; ?>

    <div class="navigation">
    <div class="next-posts"><?php next_posts_link(); ?></div>
    <div class="prev-posts"><?php previous_posts_link(); ?></div>
    </div>

    <?php else : ?>

    <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
    <h1>Not Found</h1>
    </div>

    <?php endif; ?>

    <?php wp_reset_query(); ?>

    Let me know if further assistance is required on this!

    Thank you,

    Dimitris

  • Georg
    • Design Lord, Child of Thor

    Hi Dimitris,

    I’ve updated the key input to the one for my site and your code works fine. However, I wanted to take it one step further. In a situation like this:

    person city

    Al Oslo

    Barny Bergen

    Charlie Oslo

    I was looking for the code for a display like this:

    Bergen

    – Barny

    Oslo

    – Al

    – Charlie

    Is this possible?

    Bets Regards

    Georg Inderdal

  • Dimitris Kalliris
    • Support Team Lead

    Hello Georg

    This would require to have separate loops per city, for example:

    $oslo_query = query_posts( array(
    'post_type' => 'person',
    'meta_key' => '_ct_text_5c40dd53ea847',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_query' => array(
    array(
    'key' => '_ct_text_5c40dd53ea847',
    'value' => 'Oslo'
    )
    )
    ) );

    Warm regards,

    Dimitris