how do you get the rest_post_query filter to work across multiple post types? I’d like to modify the orderby for all post types but at the moment i have to specify “post” in the filter name, which means it doesn’t work on the custom post types i’ve created?

add_filter("rest_post_query", function ($args) {

    $args["orderby"] = "menu_order";
    $args["order"] = "ASC";

    return $args;

}, 10, 2);

1 Answer
1

Define the callback as a named function and hook it separately for each post type.

function wpse_299908_order_rest_query( $args ) {
    $args['orderby'] = 'menu_order';
    $args['order'] = 'ASC';

    return $args;
}
add_filter( 'rest_post_query', 'wpse_299908_order_rest_query' );
add_filter( 'rest_page_query', 'wpse_299908_order_rest_query' );

There’s no filter that automatically applies to all endpoints, likely for the same reason that you can’t query multiple post types at once to begin with. As discussed during development of the API:

Because each custom post type is modeled differently, it’s not
possible to fetch them from the same endpoint in v2. Conceptually,
it’s like fetching users and posts from the same end point — it
doesn’t make much sense.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *