How to force a query conditional?

Did you check what part of is_front_page() is causing it to return false?

I could reproduce the problem by following the setup from the trac ticket. In my case inside this function the call to is_page() was returning false.

I guess this is due to using $wp_query->set() for page_id and is_page is only causing the query_vars to be changed, you don’t change the “state of the query” itself.

I was able to create a workaround for this by appending this two lines to your code:

$query->is_page = true;
$query->queried_object = get_post(get_option('page_on_front') );

Which resulted (in my setup) in the page being correctly displayed on the frontpage (despite the query_var being present in the URL) and also is_front_page() returning true.

I hope this is helping you somewhat further.


If you look at the definition of the WP_Query class, you’ll see that there is a variable $query_vars and a variable $is_page.

The set function you used ($query->set( 'is_page', true );) does only set a query var:

function set($query_var, $value) {
    $this->query_vars[$query_var] = $value;
}

It does not change the state information that is saved in $query->is_page = true, instead it sets the query var $query->query_vars['is_page'] = true.

That is a bit misleading if you come from an OOP-approach and understand WP_Query::set() as a classic class-setter function – which it isn’t.

Leave a Comment