• I’m currently listing all my posts from my custom_post_type by a meta_key value called ‘city’ as shown below:

    <?php
                $catListings = new WP_Query( array(
                   'post_type' => 'directory',
                   'post_parent' => $post->ID,
                   'posts_per_page' => 500,
                   'paged' => $paged,
                   'order' => ASC,
                   'orderby'=> 'meta_key=city') );
                ?>

    The problem is that there are a lot of posts with the same city. So, I need to have it sort them by name, after sorting them by city. Does anyone know how I can do this?

Viewing 1 replies (of 1 total)
  • You can use filters to sort by multiple fields. I am not sure what ‘name’ you are referring to, so I cannot give specific code. In general, the code will look like this:

    function mam_posts_orderby ($orderby) {
       global $mam_global_orderby;
       if ($mam_global_orderby) $orderby = $mam_global_orderby;
       return $orderby;
    }
    add_filter('posts_orderby','mam_posts_orderby');
    $mam_global_orderby = 'field1 ASC, field2 DESC';
    query_posts();
    $mam_global_orderby = '';  // Turn off the filter

    Can you give more detail about what you want to use for sorting?

Viewing 1 replies (of 1 total)
  • The topic ‘How to sort listing by two fields?’ is closed to new replies.