Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Liam Maclachlan
Liam Maclachlan
22,805 Points

How does WordPress know a post 'is_single'?

Okay, this is going to go deep.

I have just been browsing through the WordPress core files (as you do) and I have a question...

the function 'is_single' simply returns a boolean of true or false

<?php
function is_single( $post = '' ) {
    global $wp_query;

    if ( ! isset( $wp_query ) ) {
        _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
        return false;
    }

    return $wp_query->is_single( $post );
}
?>

Now, where in this code does it compile and/or check whether it is or not?

My assumption is that it goes in to the $wp_query object and checks to see if it has the property of 'is_single' (or some variation) and this simply has a true/false value and returns it. Can anyone point me to the line of code where the wp_query object is constructed and it defines whether or not it is a single post?

No real reason for me to know this, just interested(?) is all. WHERE DOES IT DEFINE THE INITIAL BOOLEAN TO BE ABLE TO RETURN TRUE/FALSE!?

Let me know what you think! :) Cheers.

2 Answers

Kevin Korte
Kevin Korte
28,149 Points

Let me turn you towards some reading instead, as this is going deep down the rabbit hole, and I honestly do not probably have a deep enough understanding, nor time to understand it and try to explain it clearly.

See if these articles help you:

https://codex.wordpress.org/Query_Overview

http://humanshell.net/2011/08/wordpress-initialization/

Liam Maclachlan
Liam Maclachlan
22,805 Points

Perfect. THanks you man. The line I was looking for was this one :)

"WordPress runs the wp() function (in wp-includes/functions.php), which calls $wp->main() ($wp is an object of class WP, which is defined in wp-includes/class-wp.php). This tells WordPress to:

Parse the URL into a query specification using WP->parse_request() -- more on that below.
Set all the is_ variables that are used by Conditional Tags using $wp_query->parse_query() ($wp_query is an object of class WP_Query, which is defined in wp-includes/query.php). Note that in spite of this function's name, in this case WP_Query->parse_query doesn't actually do any parsing for us, since that is done before-hand by WP->parse_request(). "
Kevin Korte
Kevin Korte
28,149 Points

Perfect, I thought that might be it! I just wasn't sure, let alone understand it enough to explain it correctly.

Liam Maclachlan
Liam Maclachlan
22,805 Points

Yeah. It's very much one question leading to another at the moment. Turns out that poking inaround the core files reallly has helped me understand when/why to call certain functions, or even lay in to certain hooks.

Thanks for the link man. Got me some bed time reading.

Kevin Korte
Kevin Korte
28,149 Points

All of this happens in wp-include/query.php.

So go here: https://core.trac.wordpress.org/browser/tags/4.2.2/src/wp-includes/query.php#L1030

Line 1030. In the wp_query class, is_single is first set to false. Later, this query class is instantiated on each post object.

Some other instances wp_query sets is_single to true -> https://core.trac.wordpress.org/browser/tags/4.2.2/src/wp-includes/query.php#L597

When is_single is called, if wp_query is NOT set, than it knows it doesn't have a valid query to even check. If a wp_query is set, it is set as false as you saw above, but we know we have a query, and we have a post (because of all of the other checks the WP_Query class does, than return the is_single to true, instead of false.

Is that short enough, but detailed enough at the same time?

Liam Maclachlan
Liam Maclachlan
22,805 Points

Hey man. Thanks for the response.

"Line 1030. In the wp_query class, is_single is first set to false. Later, this query class is instantiated on each post object." So I suppose my question really is, where is the post instantiated to make the statement true, and where does it then compare this to make it true?

EDIT: I get that the line--

<?php 
    return $wp_query->is_single( $post );
?>

returns the true/false boolean but I want to know where it checks to see where the current object/class $wp_query is holding the valuse of ->is_single.

Where/how is the $wp_query object constructed/stored with the information to put forward a true statement for 'is_single'? (I understand it is the loop that does this, but where does it pull that informaiton from)

EDIT 2: sory if it seems I'm asking the same question. I know what I'm trying to ask but can't think of another way to word it :)