admin管理员组文章数量:1300033
I'm using the rest_photo_query
filter to manipulate the arguments of WP_Query
through the use of my own GET
parameters. It's working perfectly and fast, with one exception.
I can adjust the orderby
parameter using rest_sht_photo_collection_params
, and it's easy to sort the results by meta_value
.
The photo Custom Post Type entries are connected to a second Custom Post Type species by means of the species_id
.
I need to be able to sort the photo posts by species title.
Does anyone have a good idea how to achieve this? As I'm modifying the WP_Query
in a standard endpoint, my preference would be to modify the WP_Query
arguments somehow.
I've tried building my own query in a custom endpoint and then modifying the resultant array by looping through it, but this makes the request about 100x slower.
Here's a partial example of a simpler field, where the custom orderby my_custom_meta_field
is added as a meta_value
comparison:
switch ($args['orderby']) {
case 'my_custom_meta_field':
$args['orderby'] = 'meta_value';
$args['meta_key'] = 'my_custom_meta_field';
break;
I'm using the rest_photo_query
filter to manipulate the arguments of WP_Query
through the use of my own GET
parameters. It's working perfectly and fast, with one exception.
I can adjust the orderby
parameter using rest_sht_photo_collection_params
, and it's easy to sort the results by meta_value
.
The photo Custom Post Type entries are connected to a second Custom Post Type species by means of the species_id
.
I need to be able to sort the photo posts by species title.
Does anyone have a good idea how to achieve this? As I'm modifying the WP_Query
in a standard endpoint, my preference would be to modify the WP_Query
arguments somehow.
I've tried building my own query in a custom endpoint and then modifying the resultant array by looping through it, but this makes the request about 100x slower.
Here's a partial example of a simpler field, where the custom orderby my_custom_meta_field
is added as a meta_value
comparison:
switch ($args['orderby']) {
case 'my_custom_meta_field':
$args['orderby'] = 'meta_value';
$args['meta_key'] = 'my_custom_meta_field';
break;
Share
Improve this question
asked Mar 24, 2021 at 12:57
Mark Howells-MeadMark Howells-Mead
3152 silver badges9 bronze badges
9
|
Show 4 more comments
2 Answers
Reset to default 0You can't, WP_Query
can sort by post meta value, but it can't then use that value as a post ID, look up that post, then sort by a value on that other post.
If you had a small number of posts, it could be done by manually sorting the results with PHP.
Fundamentally, the data is not stored in a way that makes this feasible.
I solved the problem by adding a custom post meta value to each photo, containing the species name, then ensuring that the meta is up-to-date by using save_post hooks on the photo and species. The query now works as required and returns the data I need in around 100ms. This means that I can continue to use this solution without having to use JavaScript.
版权声明:本文标题:wp query - How can I sort the results of a REST API response by the title of a connected custom post type? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741651045a2390480.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
WP_Query
can do, and adding levels of indirectness is not something you want to do – Tom J Nowell ♦ Commented Mar 24, 2021 at 13:29meta_query
in there too). Otherwise the pagination doesn't work. We're talking about nearly 10,000 posts, so I can't send them all to the frontend and let the browser handle the filtering and sorting. – Mark Howells-Mead Commented Mar 24, 2021 at 13:57species
then fetch posts for each species in order and using the species totals count to figure out pagination? The posts first approach is going to be extremely expensive. You're going to want to invest in a lot of caching at multiple levels to make it work at any kind of scale ( think more than a handful of concurrent users/requests ) – Tom J Nowell ♦ Commented Mar 24, 2021 at 15:43