Query_vars类别名称仅显示多个类别中的一个类别

时间:2016-04-19 作者:Sumit

我正在开发一个插件,它可以改变查询参数!插件使用$wp_query->query_vars[\'category_name\'] 获得类别Slug。当我从一个类别获取帖子时,一切都正常。但当传递逗号分隔的slug时$wp_query->query_vars[\'category_name\'] 仅显示第一个类别。

这是回路

$args = array ( 
    \'post_type\' => \'post\',
    \'posts_per_page\' => -1,
    \'category_name\' => \'postcat,postcat2\'
); 

$custom_query = new WP_Query($args);
print_r($custom_query);
这就是我打印查询时得到的结果。

WP_Query Object
(
    [query] => Array
        (
            [post_type] => post
            [posts_per_page] => -1
            [category_name] => postcat,postcat2
        )

    [query_vars] => Array
        (
            [post_type] => post
            [posts_per_page] => -1
            [category_name] => postcat
            [error] => 
            [m] => 
            [p] => 0
            [post_parent] => 
            [subpost] => 
            [subpost_id] => 
我想知道为什么$my_query->query_vars[\'category_name\'] 只有一类子弹?

非常感谢。

1 个回复
最合适的回答,由SO网友:dan9vu 整理而成

在…内get_posts() 属于WP_Query 类,您将看到此代码块。

/*
 * Ensure that \'taxonomy\', \'term\', \'term_id\', \'cat\', and
 * \'category_name\' vars are set for backward compatibility.
 */
if ( ! empty( $this->tax_query->queried_terms ) ) {

    /*
     * Set \'taxonomy\', \'term\', and \'term_id\' to the
     * first taxonomy other than \'post_tag\' or \'category\'.
     */
    if ( ! isset( $q[\'taxonomy\'] ) ) {
        foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) {
            if ( empty( $queried_items[\'terms\'][0] ) ) {
                continue;
            }

            if ( ! in_array( $queried_taxonomy, array( \'category\', \'post_tag\' ) ) ) {
                $q[\'taxonomy\'] = $queried_taxonomy;

                if ( \'slug\' === $queried_items[\'field\'] ) {
                    $q[\'term\'] = $queried_items[\'terms\'][0];
                } else {
                    $q[\'term_id\'] = $queried_items[\'terms\'][0];
                }

                // Take the first one we find.
                break;
            }
        }
    }

    // \'cat\', \'category_name\', \'tag_id\'
    foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) {
        if ( empty( $queried_items[\'terms\'][0] ) ) {
            continue;
        }

        if ( \'category\' === $queried_taxonomy ) {
            $the_cat = get_term_by( $queried_items[\'field\'], $queried_items[\'terms\'][0], \'category\' );
            if ( $the_cat ) {
                $this->set( \'cat\', $the_cat->term_id );
                $this->set( \'category_name\', $the_cat->slug );
            }
            unset( $the_cat );
        }

        if ( \'post_tag\' === $queried_taxonomy ) {
            $the_tag = get_term_by( $queried_items[\'field\'], $queried_items[\'terms\'][0], \'post_tag\' );
            if ( $the_tag ) {
                $this->set( \'tag_id\', $the_tag->term_id );
            }
            unset( $the_tag );
        }
    }
}
正如你在第二个foreach 回路,用于category 在分类法中,WordPress仅使用第一个查询的术语从中提取slug,然后将其设置为query_vars. 同样的情况也发生在post_tag.

WordPress为什么这么做,评论已经说明了一切。