apply_filters( “rest_{$this->post_type}_query”, array $args, WP_REST_Request $request )

Filters WP_Query arguments when querying posts via the REST API.

Description

The dynamic portion of the hook name, $this->post_type, refers to the post type slug.

Possible hook names include:

  • rest_post_query
  • rest_page_query
  • rest_attachment_query

Enables adding extra arguments or setting defaults for a post collection request.

Parameters

$argsarray
Array of arguments for WP_Query.
$requestWP_REST_Request
The REST API request.

Source

$args       = apply_filters( "rest_{$this->post_type}_query", $args, $request );

Changelog

VersionDescription
5.7.0Moved after the tax_query query arg is generated.
4.7.0Introduced.

User Contributed Notes

  1. Skip to note 4 content

    I wanted to see the SQL generated from the filters I’ve added. However, using &debug=sql on the http request parameter didn’t work for me.

    You can set a breakpoint at line 299 (at least at the time of writing) under class-wp-rest-posts-controller.php@get_items() method, which looks like this:

    $posts_query  = new WP_Query(); // line 298
    $query_result = $posts_query->query( $query_args ); // line 299

    Or if you don’t have a debugger setup yet, you can do:

    $posts_query  = new WP_Query();
    $query_result = $posts_query->query( $query_args );
    echo $posts_query->request;
    exit;

You must log in before being able to contribute a note or feedback.