覆盖pre_get_posts和税收查询

时间:2018-09-24 16:40:45

标签: wordpress

我的问题如下:

我有一个简单的pre_get_posts操作来过滤所有网站中所有非英语的帖子:

function pre_query_languages( $query ) {
    if ( is_admin() || $query->is_search() || is_single()  ) {
        return;
    }

    $tax_query = $query->get( 'tax_query' ) ?: array();

    $tax_query[] = array(
        'taxonomy' => 'language',
        'field'    => 'term_id',
        'terms'    => array( 28,29,30,31 ),
        'operator' => 'NOT IN',
    );

    $query->set( 'tax_query', $tax_query );
}
add_action( 'pre_get_posts', 'pre_query_languages' );

在某些页面上,我正在加载(以AJAX格式)特定语言的帖子。但是我似乎找不到改变我的查询的方法,因此原来的tax_query被完全忽略了。

即使我使用$wp_query->set('tax_query',array());, 从[query_vars] [tax_query]中删除了tax_query,但从[tax_query]中删除了。 (请参见代码)

WP_Query Object
(
    [query] => Array
        (
            [paged] => 1
            [post_status] => publish
            [suppress_filters] => 
            [posts_per_page] => 8
            [post_type] => press_coverage
            [tax_query] => Array
                (
                    [relation] => OR
                    [0] => Array
                        (
                            [taxonomy] => language
                            [terms] => 28
                        )

                )

        )

    [query_vars] => Array
        (
            [paged] => 1
            [post_status] => publish
            [suppress_filters] => 
            [posts_per_page] => 8
            [post_type] => press_coverage
            [tax_query] => Array
                (
                    [relation] => OR
                    [0] => Array
                        (
                            [taxonomy] => language
                            [terms] => 28
                        )
                    //pre_get_posts tax-query is gone here

                )

            [error] => 
            [m] => 
            [p] => 0
            [post_parent] => 
            [subpost] => 
            [subpost_id] => 
            [attachment] => 
            [attachment_id] => 0
            [name] => 
            [static] => 
            [pagename] => 
            [page_id] => 0
            [second] => 
            [minute] => 
            [hour] => 
            [day] => 0
            [monthnum] => 0
            [year] => 0
            [w] => 0
            [category_name] => 
            [tag] => 
            [cat] => 
            [tag_id] => 
            [author] => 
            [author_name] => 
            [feed] => 
            [tb] => 
            [meta_key] => 
            [meta_value] => 
            [preview] => 
            [s] => 
            [sentence] => 
            [title] => 
            [fields] => 
            [menu_order] => 
            [embed] => 
            [category__in] => Array
                (
                )

            [category__not_in] => Array
                (
                )

            [category__and] => Array
                (
                )

            [post__in] => Array
                (
                )

            [post__not_in] => Array
                (
                )

            [post_name__in] => Array
                (
                )

            [tag__in] => Array
                (
                )

            [tag__not_in] => Array
                (
                )

            [tag__and] => Array
                (
                )

            [tag_slug__in] => Array
                (
                )

            [tag_slug__and] => Array
                (
                )

            [post_parent__in] => Array
                (
                )

            [post_parent__not_in] => Array
                (
                )

            [author__in] => Array
                (
                )

            [author__not_in] => Array
                (
                )

            [ignore_sticky_posts] => 
            [cache_results] => 1
            [update_post_term_cache] => 1
            [lazy_load_term_meta] => 1
            [update_post_meta_cache] => 1
            [nopaging] => 
            [comments_per_page] => 50
            [no_found_rows] => 
            [taxonomy] => language
            [term_id] => 28
            [order] => DESC
        )
    // but not here !
    [tax_query] => WP_Tax_Query Object
        (
            [queries] => Array
                (
                    [relation] => OR
                    [0] => Array
                        (
                            [taxonomy] => language
                            [terms] => Array
                                (
                                    [0] => 28
                                )

                            [field] => term_id
                            [operator] => IN
                            [include_children] => 1
                        )

                    [1] => Array
                        (
                            [taxonomy] => language
                            [terms] => Array
                                (
                                    [0] => 28
                                    [1] => 29
                                    [2] => 30
                                    [3] => 31
                                )

                            [field] => term_id
                            [operator] => NOT IN


            [include_children] => 1
                    )

            )

        [relation] => OR
        [table_aliases:protected] => Array
            (
                [0] => wpfw_term_relationships
            )

        [queried_terms] => Array
            (
                [language] => Array
                    (
                        [terms] => Array
                            (
                                [0] => 28
                            )

                        [field] => term_id
                    )

            )

        [primary_table] => wpfw_posts
        [primary_id_column] => ID
    )

我尝试使用get_posts,来自pre_get_posts的税收查询也处于活动状态,然后出现相同的问题。

是否有任何方法可以从查询中的pre_get_posts中覆盖税收查询(无需使用SQL),也许是在pre_get_posts操作中直接调用的条件?我是遇到错误还是做错了?

0 个答案:

没有答案