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.

Actually a simple question, but I need to be sure that assigning a variable for the result of a function can help to increase the speed of a site e.g.

The function: if is_paged(){ $paged = 1; }

The replacement: if $paged == "1" .. then do - RATHER THAN USING THE SAME FUNCTION over and over

W3 total cache would most likely cache it, but sometimes users hit pages that are not cached and I think it could increase the speed of a website, is my assumption correct?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

So, all query conditionals (is_paged, is_singular, etc) look something like this:

function is_paged() {
    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_paged();
}

Globalize $wp_query and call the corresponding method of the WP_Query object. In this case:

<?php
function is_paged() {
    return (bool) $this->is_paged;
}

The only database hit you incur is from when the WP_Query object is created, which happens anyway.

Calling a function incurs a cost, so does using an if statement. Those costs on today's hardware are nothing to really worry about.

That said, if you find yourself doing a lot of conditional checking using the same function, you're probably better off to evaluate the control flow of your program.

Something like this:

<?php
if(is_paged() && 1 == get_query_var('some_query_var'))
{
   // do stuff
}
else if(is_paged() && 2 == get_query_var('some_query_var'))
{
   // do other stuff
}

Could become:

<?php
if(is_paged())
{
   switch(get_query_var('some_query_var'))
   {
      case 1:
          // do stuff
          break;
      case 2:
          // do stuff
          break;
      default:
          // do stuff
          break;
    }
}

As a final word of advice, I would "cache" results of conditionals (or any other function call, within reason) within the same function if you need them more than once, but I wouldn't cache something like a object property unless you really need that persist from method to method.

Here's a terrible example that (probably) doesn't follow my advice above.

<?php
function wpse65916_stuff()
{
    $paged = is_paged();

    if($paged)
    {
        // do stuff because paged
    }

    if($paged && /* some other condition */)
    {
        // do stuff
    }
}
share|improve this answer

No.

Query conditionals are part of WordPress' object cache, and incur no additional resources when called.

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.