Custom arguments in WP_Query

时间:2016-06-11 作者:Diksha Anand

我正在研究Wordpress主题,即目录引擎。它在标题中具有基于分类术语slug的过滤功能。我只想知道这个tax\\u查询是从哪里为WP\\u查询生成的。这是代码。

 $this->add_action(\'pre_get_posts\', \'pre_get_places\');
 function pre_get_places($query) {
        global $current_user;

        if ($query->is_main_query() && is_author()) {
            $query->set(\'post_type\', $this->post_type);
            if ($current_user->user_login == $query->query[\'author_name\']) {
                $query->set(\'post_status\', array(
                    \'pending\',
                    \'reject\',
                    \'archive\',
                    \'draft\',
                    \'publish\'
                ));
            }
        }
        // set default post type query in place category, location listing
        if ($query->is_main_query() && (is_tax(\'place_category\') || is_tax(\'location\') || is_tax(\'age_from\') || is_tax(\'place_type\') || is_tax(\'daddy_lounge\') || is_tax(\'place_caregivers\'))) {
            $query->set(\'post_type\', $this->post_type);
            $query->set(\'post_status\', \'publish\');
        }

        /**
         * is main query and cach request showposts
         */
        if (isset($_REQUEST[\'showposts\']) && $query->is_main_query()) {
            $query->set(\'showposts\', $_REQUEST[\'showposts\']);
        }

        /**
         * if is post type archive set status to publish
         */
        if (is_post_type_archive($this->post_type) && !is_admin()) {
            if (!isset($query->query_vars[\'post_status\'])) {
                $query->set(\'post_status\', \'publish\');
            }
        }

        /**
         * filter orderby
         */
        if (isset($_REQUEST[\'sortby\']) 
            && ( $query->is_main_query() || (isset($query->query_vars[\'meta_key\']) && $query->query_vars[\'meta_key\'] == \'rating_score\') )
        ) {
            if ($_REQUEST[\'sortby\'] !== \'date\') {
                // $query->query_vars[\'meta_key\'] = $_REQUEST[\'sortby\'];
                $query->set(\'orderby\', \'meta_value_num\');
            } else {

                // order by date
                $query->set(\'orderby\', \'date\');
            }

            // order desc
            $query->set(\'order\', \'DESC\');
        }
        // order by rating score
        if(isset($_REQUEST[\'query\'][\'orderby\']) && $_REQUEST[\'query\'][\'orderby\'] == \'rating_score\') {
            $query->set(\'orderby\', \'meta_value_num date\');
            if( !isset($_REQUEST[\'query\'][\'meta_key\'] ) ) {
                $query->query_vars[\'meta_key\'] = $_REQUEST[\'query\'][\'orderby\'];
                $query->meta_query = array(
                     //check to see if et_featured has been filled out
                    \'relation\' => \'OR\',
                    array(
                         //check to see if date has been filled out
                        \'key\' => $_REQUEST[\'query\'][\'orderby\'],
                        \'compare\' => \'BETWEEN\',
                        \'value\' => array(
                            0,
                            5
                        )
                    ),
                    array(
                         //if no et_featured has been added show these posts too
                        \'key\' => $_REQUEST[\'query\'][\'orderby\'],
                        \'value\' => \'\',
                        \'compare\' => \'NOT EXISTS\'
                    )
                );
            }else{
                // order by rating score for feature block
                if($_REQUEST[\'query\'][\'meta_key\'] == \'et_featured\') {
                    $query->set(\'orderby\', \'menu_order\');
                    $query->set(\'meta_query\' , array(
                        array(
                            \'key\' => \'rating_score\',
                            // \'value\' => array(0, 5),
                            // \'compare\' => \'BETWEEN\'
                        ),
                        array(
                            \'key\' => \'et_featured\',
                            \'value\' => \'1\',
                            \'compare\' => \'LIKE\'
                        )
                    )); 
                }
                if($_REQUEST[\'query\'][\'meta_key\'] == \'de_event_post\') {
                    // order by rating score for event block
                    $query->set(\'orderby\', \'menu_order\');
                    $query->set(\'meta_query\' , array(
                        array(
                            \'key\' => \'rating_score\',
                            \'value\' => array(0, 5),
                            \'compare\' => \'BETWEEN\'
                        ),
                        array(
                            \'key\' => \'de_event_post\',
                            \'value\' => \'\',
                            \'compare\' => \'!=\', 
                            \'type\' => \'NUMERIC\'
                        )
                    )); 
                }            
            }
        }

        $is_search = ! (empty($_REQUEST[\'query\']) || empty($_REQUEST[\'query\'][\'s\']));
        if ((et_load_mobile() && !is_single() ) && !$is_search) {
            if (!is_author() && isset($query->query_vars[\'post_type\']) && $query->query_vars[\'post_type\'] == $this->post_type) {
                if(!is_search()){
                    $query->set(\'meta_key\', \'et_featured\'); 
                }
                if(!isset($query->query_vars[\'near_lat\']) || $query->query_vars[\'near_lat\'] == \'\' ){
                    $query->set(\'orderby\', \'meta_value_num date\');
                }
                if (isset($query->query_vars[\'meta_value\'])) {
                    unset($query->query_vars[\'meta_value\']);
                }                
                $query->meta_query = array(
                     //check to see if et_featured has been filled out
                    \'relation\' => \'OR\',
                    array(
                         //check to see if date has been filled out
                        \'key\' => \'et_featured\',
                        \'compare\' => \'IN\',
                        \'value\' => array(
                            0,
                            1
                        )
                    ) ,
                    array(
                         //if no et_featured has been added show these posts too
                        \'key\' => \'et_featured\',
                        \'value\' => 1,
                        \'compare\' => \'NOT EXISTS\'
                    )
                );
            }
        }

        return $query;
    }

 $args = array(
    "post_type" => "place",
    "post_status" => "publish",
    "orderby" => "date",
    "place_category" => "",
    "location" => "",
    "showposts" => "4",
    "order" => "DESC",
    "meta_key" => "et_featured",
    "meta_value" => "1",
    "paginate" => "page",
    "age_from" => "2",
    "age_to" => "5",
    "place_caregivers" => "no",
    "place_type" => "indoor",
    "daddy_lounge" => "no",
    "paged" => "1",
    "thumbnail" => "big_post_thumbnail",
    "posts_per_page" => false
};
$query = new WP_Query($args);
$查询返回

"data":null,
"paginate":"<script type=\\"application\\/json\\" class=\\"ae_query\\">{\\"post_type\\":\\"place \\",\\"post_status\\":\\"publish\\",\\"orderby\\":\\"date\\",\\"place_category\\":\\"\\",\\"location\\":\\"\\",\\"showposts \\":\\"4\\",\\"order\\":\\"DESC\\",\\"meta_key\\":\\"et_featured\\",\\"meta_value\\":\\"1\\",\\"paginate\\":\\"page\\", \\"age_from\\":\\"2\\",\\"age_to\\":\\"5\\",\\"place_caregivers \\":\\"no\\",\\"place_type\\":\\"indoor\\",\\"daddy_lounge\\":\\"no\\",\\"paged\\":\\"1\\",\\"thumbnail\\":\\"big_post_thumbnail \\",\\"posts_per_page\\":false}<\\/script>",
"msg":"Successs",
"success":true,
"max_num_pages":null,
"status":"0 place",
"total":0,
"total_status":{

    "publish":"71",
    "pending":0,
    "archive":"5",
    "reject":"1",
    "draft":"1"

},
"data_query":{

    "query":{
        "query":{
            "post_type":"place",
            "post_status":"publish",
            "orderby":"date",
            "place_category":"",
            "location":"",
            "showposts":"4",
            "order":"DESC",
            "meta_key":"et_featured",
            "meta_value":"1",
            "paginate":"page",
            "age_from":"2",
            "age_to":"5",
            "place_caregivers":"no",
            "place_type":"indoor",
            "daddy_lounge":"no",
            "paged":"1",
            "thumbnail":"big_post_thumbnail",
            "posts_per_page":false
        },
        "query_vars":{
            "post_type":"place",
            "post_status":"publish",
            "orderby":"date",
            "place_category":"",
            "location":"",
            "showposts":4,
            "order":"DESC",
            "meta_key":"et_featured",
            "meta_value":"1",
            "paginate":"page",
            "age_from":"2",
            "age_to":"5",
            "place_caregivers":"no",
            "place_type":"indoor",
            "daddy_lounge":"no",
            "paged":1,
            "thumbnail":"big_post_thumbnail",
            "posts_per_page":4,
            "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":"",
            "comments_popup":"",
            "preview":"",
            "s":"",
            "sentence":"",
            "fields":"",
            "menu_order":"",
            "category__in":[
            ],
            "category__not_in":[
            ],
            "category__and":[
            ],
            "post__in":[
            ],
            "post__not_in":[
            ],
            "tag__in":[
            ],
            "tag__not_in":[
            ],
            "tag__and":[
            ],
            "tag_slug__in":[
            ],
            "tag_slug__and":[
            ],
            "post_parent__in":[
            ],
            "post_parent__not_in":[
            ],
            "author__in":[
            ],
            "author__not_in":[
            ],
            "ignore_sticky_posts":false,
            "suppress_filters":false,
            "cache_results":true,
            "update_post_term_cache":true,
            "update_post_meta_cache":true,
            "nopaging":false,
            "comments_per_page":"50",
            "no_found_rows":false,
            "taxonomy":"daddy_lounge",
            "term":"no"
        },
        "tax_query":{
            "queries":[
                {
                    "taxonomy":"age_from",
                    "terms":"2",
                    "field":"slug",
                    "operator":"IN",
                    "include_children":true
                },
                {
                    "taxonomy":"age_to",
                    "terms":"5",
                    "field":"slug",
                    "operator":"IN",
                    "include_children":true
                },
                {
                    "taxonomy":"place_caregivers",
                    "terms":[
                        "no"
                    ],
                    "field":"slug",
                    "operator":"IN",
                    "include_children":true
                },
                {
                    "taxonomy":"place_type",
                    "terms":[
                        "indoor"
                    ],
                    "field":"slug",
                    "operator":"IN",
                    "include_children":true
                },
                {
                    "taxonomy":"daddy_lounge",
                    "terms":[
                        "no"
                    ],
                    "field":"slug",
                    "operator":"IN",
                    "include_children":true
                }
            ],
            "relation":"AND",
            "queried_terms":{
                "age_from":{
                    "terms":"2",
                    "field":"slug"
                },
                "age_to":{
                    "terms":"5",
                    "field":"slug"
                },
                "place_caregivers":{
                    "terms":[
                        "no"
                    ],
                    "field":"slug"
                },
                "place_type":{
                    "terms":[
                        "indoor"
                    ],
                    "field":"slug"
                },
                "daddy_lounge":{
                    "terms":[
                        "no"
                    ],
                    "field":"slug"
                }
            },
            "primary_table":"wp_posts",
            "primary_id_column":"ID"
        },
        "meta_query":{
            "queries":{
                "0":{
                    "key":"et_featured",
                    "value":"1"
                },
                "relation":"OR"
            },
            "relation":"AND",
            "meta_table":"wp_postmeta",
            "meta_id_column":"post_id",
            "primary_table":"wp_posts",
            "primary_id_column":"ID"
        },
        "date_query":false,
        "request":"SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id) INNER JOIN wp_term_relationships AS tt2 ON (wp_posts.ID = tt2 .object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( \\n 0 = 1 \\n AND \\n 0 = 1 \\n AND \\n wp_term_relationships.term_taxonomy_id IN (130) \\n AND \\n tt1.term_taxonomy_id IN (125) \\n AND \\n tt2.term_taxonomy_id IN (128)\\n) AND ( \\n ( wp_postmeta.meta_key = \'et_featured\' AND CAST(wp_postmeta.meta_value AS CHAR) = \'1\' )\\n) AND wp_posts.post_type = \'place\' AND ((wp_posts .post_status = \'publish\')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 4",
        "posts":[
        ],
        "post_count":0,
        "current_post":-1,
        "in_the_loop":false,
        "comment_count":0,
        "current_comment":-1,
        "found_posts":0,
        "max_num_pages":0,
        "max_num_comment_pages":0,
        "is_single":false,
        "is_preview":false,
        "is_page":false,
        "is_archive":true,
        "is_date":false,
        "is_year":false,
        "is_month":false,
        "is_day":false,
        "is_time":false,
        "is_author":false,
        "is_category":false,
        "is_tag":false,
        "is_tax":true,
        "is_search":false,
        "is_feed":false,
        "is_comment_feed":false,
        "is_trackback":false,
        "is_home":false,
        "is_404":false,
        "is_comments_popup":false,
        "is_paged":false,
        "is_admin":true,
        "is_attachment":false,
        "is_singular":false,
        "is_robots":false,
        "is_posts_page":false,
        "is_post_type_archive":true,
        "thumbnails_cached":false
    }
实际上,我希望或联系“age\\u to”分类法和“age\\u from”分类法,并希望和其他分类法(如“place\\u Carders”、“place\\u type”和“daddy\\u lounge”)建立联系,但无法找到任何编辑tax\\u查询的方法。有什么建议吗?提前谢谢。

1 个回复
SO网友:TheDeadMedic

WordPress正在为您生成税务查询-当您注册自定义分类法时,它也会注册称为查询变量的内容。这是一个“键”类型的值,意味着您可以简单地执行以下操作:

new WP_Query([ \'age_from\' => 2 ]);
。。。WP将其转化为:

WP_Tax_Query([
    [
        \'taxonomy\' => \'age_from\',
        \'terms\'    => 2,
        \'field\'    => \'slug\',
    ]
]);
。。。但不是在之前pre_get_posts 火灾(在线发生2802 属于wp-includes/query.php). 我的建议是在您的参数中使用直接税查询,而不是分类查询变量,这样您就可以完全控制关系:

$args = array(
    \'post_type\'   => \'place\',
    \'post_status\' => \'publish\',
    \'orderby\'     => \'date\',
    \'tax_query\'   => array(
        \'relation\' => \'AND\',

        array(
            \'relation\' => \'OR\',
            array(
                \'taxonomy\' => \'age_from\',
                \'terms\'    => \'2\',
            ),
            array(
                \'taxonomy\' => \'age_to\',
                \'terms\'    => \'5\',
            ),
        ),

        array(
            \'relation\' => \'AND\',
            array(
                \'taxonomy\' => \'place_caregivers\',
                \'terms\'    => \'no\',
            ),
            array(
                \'taxonomy\' => \'place_type\',
                \'terms\'    => \'indoor\',
            ),
            array(
                \'taxonomy\' => \'daddy_lounge\',
                \'terms\'    => \'no\',
            ),
        ),
    ),
);
阅读关于"nested" relations in WordPress 4.1

相关推荐