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
1 Answer
Reset to default 1Yes 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 ofpost, cats, dogs, rabbits
(note thepost
which is the first item in thepost_type
array).
本文标签: Query multiple post typesOrder by type and post title title
版权声明:本文标题:Query multiple post types - Order by type and post title title 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736688574a1947794.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论