更新到 WP 4.7 时忽略了 WordPress API V2 的额外参数和查询

Extra arguments and queries for the WordPress API V2 ignored with update to WP 4.7

我有一个网站一直在使用 WordPress REST API V2 插件。我使用下面的代码添加了一个额外的参数(过滤器),我可以在调用使用自定义分类法 topics 标记的帖子时使用它。该网站需要能够将多个分类术语添加到查询中,并显示具有任何指定术语的任何帖子,但仅显示具有其中一个指定术语的帖子。

add_action( 'rest_query_vars', 'custom_multiple_topics' );
function custom_multiple_topics( $vars ) {
    array_push( $vars, 'tax_query' );
    return $vars;
}

add_action( 'rest_post_query', 'custom_topic_query', 10, 2 );
function custom_topic_query( $args, $request ) {

    if ( isset($args[ 'topics' ]) ) {
        $pre_tax_query = array(
            'relation' => 'OR'
        );

        $topics = explode( ',', $args['topics'] );  // NOTE: Assumes comma separated taxonomies
        for ( $i = 0; $i < count( $topics ); $i++) {
            array_push( $pre_tax_query, array(
                'taxonomy' => 'topic',
                'field' => 'slug',
                'terms' => array( $topics[ $i ] )
            ));
        }

        $tax_query = array(
            'relation' => 'AND',
            $pre_tax_query
        );

        unset( $args[ 'topics' ] );  // We are replacing with our tax_query
        $args[ 'tax_query' ] = $tax_query;
    }

} // end function

一个示例 API 调用将是这样的:http://example.com/wp-json/wp/v2/posts?per_page=10&page=1&filter[topics]=audit,data

这一切都很好,直到更新到 WordPress 4.7。更新后这些参数将被忽略。我不确定从哪里开始解决这个问题。站点上没有错误 PHP 或 Javascript,自定义过滤器被忽略。更新后,所有帖子都使用此查询显示,无论它们被标记为什么。

有人 运行 解决过这个更新问题吗?

我找到了这个问题的解决方案。结果发现更新后不再使用rest_query_vars这个动作

解决方法很简单。我必须更新在 rest_post_query 操作上触发的代码以测试 $request 而不是 $args.

这是我的解决方案,它替换了问题中的所有代码:

add_action( 'rest_post_query', 'custom_topic_query', 10, 2 );
function custom_topic_query( $args, $request ) {

    if ( isset($request['filter']['topics']) ) {
        $pre_tax_query = array(
            'relation' => 'OR'
        );

        $topics = explode( ',', $request['filter']['topics'] );  // NOTE: Assumes comma separated taxonomies
        for ( $i = 0; $i < count( $topics ); $i++) {
            array_push( $pre_tax_query, array(
                'taxonomy' => 'topic',
                'field' => 'slug',
                'terms' => array( $topics[ $i ] )
            ));
        }

        $tax_query = array(
            'relation' => 'AND',
            $pre_tax_query
        );

        $args[ 'tax_query' ] = $tax_query;
    }

} // end function

请注意,我将每个 $args[ 'topics' ] 替换为 $request['filter']['topics']