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?
So, all query conditionals (
is_paged
,is_singular
, etc) look something like this:Globalize
$wp_query
and call the corresponding method of the WP_Query object. In this case: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:
Could become:
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.
No.
Query conditionals are part of WordPress’ object cache, and incur no additional resources when called.