wp query – How can I sort the results of a REST API response by the title of a connected custom post type?


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';

Mark Howells-Mead 2 years 2021-03-24T07:57:27-05:00 0 Answers 0 views 0

Leave an answer