Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I know that get_queried_object contains the object that was queried for the current page. My question is, what methods of querying affect the contents of this?

For instance:

  • query_posts
  • get_posts
  • get_post
  • new WP_Query
  • etc.
share|improve this question

2 Answers 2

up vote 1 down vote accepted

As per its laconic source:

function get_queried_object() {
    global $wp_query;
    return $wp_query->get_queried_object();
}

This function retrieves object from main query. As such it is affected by anything that changes main query. From your list that would be query_posts() (reason number umpteen it should not be used).

share|improve this answer
    
In this case, it is my hope that get_queried_object would change. –  Tyler Carter Feb 15 '13 at 16:12

All those query calls you mentioned are wrappers around WP_Query ( except wp_query which is itself WP_Query, no wrappers necessary )

So get_queried_object refers to the main query and calls $wp_query->get_queried_object();

Which we can find here:

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;
}

So the arguments passed in and how many iterations of the loop you've gone through are the determining factors.

TDLR: Just followed the code, most IDEs have a jump to definition button that would take you directly there

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.