Setting get_queried_object

时间:2013-02-14 作者:Tyler Carter

我知道get_queried_object 包含为当前页面查询的对象。我的问题是,哪些查询方法会影响此内容?

例如:

  • query_posts
  • get_posts
  • get_post
  • new WP_Query
2 个回复
最合适的回答,由SO网友:Rarst 整理而成

根据其简洁的来源:

function get_queried_object() {
    global $wp_query;
    return $wp_query->get_queried_object();
}
此函数用于从主查询中检索对象。因此,它受任何更改主查询的内容的影响。从你的列表中query_posts() (不应使用原因编号umpteen)。

SO网友:Tom J Nowell

您提到的所有查询调用都是包装器WP_Query (除了本身是wp\\U查询的wp\\U查询外,不需要包装器)

所以get_queried_object 指主查询和调用$wp_query->get_queried_object();

我们可以在这里找到:

http://core.trac.wordpress.org/browser/tags/3.5.1//wp-includes/query.php#L2987

function get_queried_object() {
    if ( isset($this->queried_object) )
        return $this->queried_object;

    $this->queried_object = null;
    $this->queried_object_id = 0;

    if ( $this->is_category || $this->is_tag || $this->is_tax ) {
        $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( \'operator\' => \'NOT IN\' ), \'NOT\' );

        $query = reset( $tax_query_in_and );

        if ( \'term_id\' == $query[\'field\'] )
            $term = get_term( reset( $query[\'terms\'] ), $query[\'taxonomy\'] );
        elseif ( $query[\'terms\'] )
            $term = get_term_by( $query[\'field\'], reset( $query[\'terms\'] ), $query[\'taxonomy\'] );

        if ( ! empty( $term ) && ! is_wp_error( $term ) )  {
            $this->queried_object = $term;
            $this->queried_object_id = (int) $term->term_id;

            if ( $this->is_category )
                _make_cat_compat( $this->queried_object );
        }
    } elseif ( $this->is_post_type_archive ) {
        $this->queried_object = get_post_type_object( $this->get(\'post_type\') );
    } elseif ( $this->is_posts_page ) {
        $page_for_posts = get_option(\'page_for_posts\');
        $this->queried_object = get_post( $page_for_posts );
        $this->queried_object_id = (int) $this->queried_object->ID;
    } elseif ( $this->is_singular && !is_null($this->post) ) {
        $this->queried_object = $this->post;
        $this->queried_object_id = (int) $this->post->ID;
    } elseif ( $this->is_author ) {
        $this->queried_object_id = (int) $this->get(\'author\');
        $this->queried_object = get_userdata( $this->queried_object_id );
    }

    return $this->queried_object;
}
所以传入的参数和循环的迭代次数是决定因素。

TDLR:只要遵循代码,大多数IDE都有一个跳转到定义按钮,可以直接将您带到那里

结束

相关推荐

JQuery.accordion即使在排队时也不是一个函数

我正在尝试为我们的客户实现一个jQuery手风琴。基本上,这是本教程(http://wp.tutsplus.com/tutorials/creative-coding/create-an-faq-accordion-for-wordpress-with-jquery-ui/) 但是在排队jQuery时遇到了问题。基本上,这是试图将教程中的手风琴脚本排队的代码:-add_action( \'wp_enqueue_scripts\', \'fl_enqueue\' ); function f