admin管理员组

文章数量:1126452

I have three custom post types which I need to retrieve posts for within one wp_query. I am seeking to return the posts in the order of post type, as well as by post title.

Within the question Ordering Custom Post Types with WP_Query @birgire provided a fantastic function which allows me to return the posts in an order as defined by an array within the post_type variable.

But I would also like to ensure that these posts are also ordered by post title. So my return would look something like this...

Cats - British Shorthair
Cats - Maine Coon
Cats - Siamese
Dogs - German Shepherd
Dogs - Labrador
Rabbits - Flemish
Rabbits - Holland Lop
Rabbits - Netherland Dwarf

I am unfamiliar with @birgire 's function and the FIELD part of it. Is it easily extendable to allow for post title sorting also?

I have three custom post types which I need to retrieve posts for within one wp_query. I am seeking to return the posts in the order of post type, as well as by post title.

Within the question Ordering Custom Post Types with WP_Query @birgire provided a fantastic function which allows me to return the posts in an order as defined by an array within the post_type variable.

But I would also like to ensure that these posts are also ordered by post title. So my return would look something like this...

Cats - British Shorthair
Cats - Maine Coon
Cats - Siamese
Dogs - German Shepherd
Dogs - Labrador
Rabbits - Flemish
Rabbits - Holland Lop
Rabbits - Netherland Dwarf

I am unfamiliar with @birgire 's function and the FIELD part of it. Is it easily extendable to allow for post title sorting also?

Share Improve this question asked Jan 16, 2024 at 11:56 Allen TullettAllen Tullett 677 bronze badges 1
  • Glad to see you got it covered by a great answer by Sally. – birgire Commented Jan 18, 2024 at 21:56
Add a comment  | 

1 Answer 1

Reset to default 1

Yes it is, and to also sort by the post title, you would just need to add , {$wpdb->posts}.post_title <ASC or DESC> after the FIELD(), like so which results in a clause that looks like ORDER BY FIELD(wp_posts.post_type, <types>), wp_posts.post_title ASC:

// Replace this:
return $orderby       = "FIELD( {$wpdb->posts}.post_type," . $post_type__in_string . ' )';

// with this:
return $orderby       = "FIELD( {$wpdb->posts}.post_type," . $post_type__in_string . ' )' . // wrapped
    ", {$wpdb->posts}.post_title ASC";

But as for sorting the posts by the post type, if you do not actually need to sort the posts in the same order you have them in the post_type array, then you could simply use an orderby array like so:

$args = array(
    'post_type' => array( 'post', 'cats', 'dogs', 'rabbits' ),
    'orderby'   => array(
        'post_type'  => 'ASC', // Sort by the post type.
        'post_title' => 'ASC', // Then, by the post title.
    ),
);

$query = new WP_Query( $args );
  • With the above example, the posts would be sorted like so:

    • Cats A
    • Cats B
    • Dogs A
    • Dogs B
    • Post A
    • Post B
    • Rabbits A
    • Rabbits B

    I.e. You would get cats, dogs, post, rabbits instead of post, cats, dogs, rabbits (note the post which is the first item in the post_type array).

本文标签: Query multiple post typesOrder by type and post title title