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

Filters the query arguments for a request.


Description Description

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


Top ↑

Parameters Parameters

$args

(array) Key value array of query var to query value.

$request

(WP_REST_Request) The request used.


Top ↑

Source Source

File: wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
4.7.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Haris

    Code example:

    function query_book_by_year($args, $request) {
    	if(isset($request["year"]) && intval($request["year"]) > 1500) {
    		$args['meta_key'] = 'year';
    		$args['meta_value'] = intval($request["year"]);
    	}		
    
    	return $args;
    }
    add_filter('rest_book_query', 'query_book_by_year', 10, 2);
    
  2. Skip to note 2 content
    Contributed by yansern

    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.