Make WordPress Core

Changeset 49923


Ignore:
Timestamp:
01/03/2021 02:37:23 AM (4 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Move the WP_Query args filter after the tax_query is setup.

This ensures that the entire list of WP_Query args are filterable in the posts controller.

Props Krstarica, TimothyBlynJacobs.
Fixes #42762.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r49790 r49923  
    263263        }
    264264
    265         // Force the post_type argument, since it's not a user input variable.
    266         $args['post_type'] = $this->post_type;
    267 
    268         /**
    269          * Filters WP_Query arguments when querying users via the REST API.
    270          *
    271          * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
    272          *
    273          * Possible filter names include:
    274          *
    275          *  - `rest_post_query`
    276          *  - `rest_page_query`
    277          *  - `rest_attachment_query`
    278          *
    279          * Enables adding extra arguments or setting defaults for a post collection request.
    280          *
    281          * @since 4.7.0
    282          *
    283          * @link https://developer.wordpress.org/reference/classes/wp_query/
    284          *
    285          * @param array           $args    Array of arguments to be passed to WP_Query.
    286          * @param WP_REST_Request $request The REST API request.
    287          */
    288         $args       = apply_filters( "rest_{$this->post_type}_query", $args, $request );
    289         $query_args = $this->prepare_items_query( $args, $request );
    290 
    291265        $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
    292266
    293267        if ( ! empty( $request['tax_relation'] ) ) {
    294             $query_args['tax_query'] = array( 'relation' => $request['tax_relation'] );
     268            $args['tax_query'] = array( 'relation' => $request['tax_relation'] );
    295269        }
    296270
     
    300274
    301275            if ( ! empty( $request[ $base ] ) ) {
    302                 $query_args['tax_query'][] = array(
     276                $args['tax_query'][] = array(
    303277                    'taxonomy'         => $taxonomy->name,
    304278                    'field'            => 'term_id',
     
    309283
    310284            if ( ! empty( $request[ $tax_exclude ] ) ) {
    311                 $query_args['tax_query'][] = array(
     285                $args['tax_query'][] = array(
    312286                    'taxonomy'         => $taxonomy->name,
    313287                    'field'            => 'term_id',
     
    318292            }
    319293        }
     294
     295        // Force the post_type argument, since it's not a user input variable.
     296        $args['post_type'] = $this->post_type;
     297
     298        /**
     299         * Filters WP_Query arguments when querying users via the REST API.
     300         *
     301         * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
     302         *
     303         * Possible filter names include:
     304         *
     305         *  - `rest_post_query`
     306         *  - `rest_page_query`
     307         *  - `rest_attachment_query`
     308         *
     309         * Enables adding extra arguments or setting defaults for a post collection request.
     310         *
     311         * @since 4.7.0
     312         * @since 5.7.0 Moved after the `tax_query` query arg is generated.
     313         *
     314         * @link https://developer.wordpress.org/reference/classes/wp_query/
     315         *
     316         * @param array           $args    Array of arguments to be passed to WP_Query.
     317         * @param WP_REST_Request $request The REST API request.
     318         */
     319        $args       = apply_filters( "rest_{$this->post_type}_query", $args, $request );
     320        $query_args = $this->prepare_items_query( $args, $request );
    320321
    321322        $posts_query  = new WP_Query();
Note: See TracChangeset for help on using the changeset viewer.