1

I have made a new query after I've asked a question about my query_posts code... you guys didn't find that great, so I decided to change that.

I made this:

$page = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$rand = new WP_Query("showposts=10&orderby=rand&paged=$page"); 
while($rand->have_posts()) : $rand->the_post();

My question is: how can I improve this?

Isn't it better to use an array? So the same code, with an array or not?

So like this:

$rand = new WP_Query( array(
    'showposts' => '10',
    'orderby' => rand
) );

Would this make my performance better?

And I read something about adding:

'update_post_meta_cache' => false

To improve the performance if terms are not going to be used. My question: what are terms anyway? Are they trying to say tags and / or categories with this?

Edit:

function random_posts( $query ) {
    if ( $query->is_home() && !$query->is_paged() && $query->is_main_query() ) {
        $query->set( 'orderby', 'rand' );
    }
}

What if I would use this? Do I have to change my WP_Query or do I have to remove it? I don't get what pre_get_posts does actually.

  • If you're worried about the impact on server, I would recommend to order queried objects randomly in front-end via JS and let the browser/user's device take the hit (1 person with single device vs 1000 or 10 000 people with one server - that's a big difference). Obviously there are limitations like with everything. JS is super fast but it's limitations are set by user's device. – N00b Feb 22 '16 at 0:26
  • Thanks man. I appreciate that thought. Have to it with PHP, for now, though. – Johan Feb 22 '16 at 0:44
  • Is this pre-optimization or is this random query currently slowing down your site? Do you see much difference compared to e.g. date ordering? Here's my answer for an alternative random ordering, that I based on an article by Josh Hartman, that might or might not work for you? – birgire Feb 22 '16 at 0:48
  • It isn't slowing it down very much, but I read that WP Query by defaults adds 5 queries in comparison to query_posts for example. So I assumed that this could be done quicker. Thanks for your post btw, I will check that out as well. – Johan Feb 22 '16 at 1:39
2

The problem with orderby rand is that on most setups the database will load all posts into memory, then randomly order them, and return what you asked for. It's that loading into memory that makes the query super slow/expensive

Also yes, terms are individual categories and tags, where category is a taxonomy, and the 'development' category is a term in that taxonomy.

Finally, your initial code snippet implies you're using a page template or attempting to override what's shown on the page. This is bad as it discards the main query and launches a second query. The cost of the first query still occurs, but it's discarded so it's a waste of time. This is why query_posts is considered harmful, replacing it with a custom WP_Query loop doesn't fix that. Consider using pre_get_posts instead to intercept the main query and change it

  • Hmm, interesting. Thanks for the answer, but it does not answer my questions... I get it that it's even better to avoid WP_Query and use something else instead, but what about the array question? How can I optimize the code I have beside changing WP_Query? And once again: I get your point, but I need a different answer for this one. – Johan Feb 22 '16 at 0:17
  • Actually you convinced me to use pre_get_posts, but I don't get the logic. Does my WP_Query have to stay the same? Or do I have to add something to functions.php and remove my WP_Query fully? – Johan Feb 22 '16 at 0:26
  • 1
    pre_get_posts is a filter, it'll let you intercept the arguments for the main query before the query happens, letting you change it. I recommend you look up the codex entry and read through ( btw these questions would all be good questions to ask on this site, by asking them in comments you're depriving yourself of better answers and reputation for bounties and priviledges ). – Tom J Nowell Feb 22 '16 at 16:13
  • 2
    The point of using pre_get_posts is that you can avoid using WP_Query, aka the second query. It's like asking WordPress to go the shop and bring back a hot chocolate, then waiting. When WordPress comes back, you throw the hot chocolate on the floor and scream "I ACTUALLY WANTED A LATTE", and send it back out, then wait. When WordPress comes back with a latte you complain it took a long time. That's what you're doing when you don't use the main loop and rely on WP_Query or query_posts to display everything. Better to say the right thing in the first place – Tom J Nowell Feb 22 '16 at 16:14
  • 1
    Of note, pre_get_posts isn't a query, and you shouldn't think of it that way. It's a way to change what the main query retrieves, it's not a new query. When using it, you should use a standard loop, not a custom WP_Query loop, e.g. while( have_posts() ) { the_post(); ... } etc. Also don't forget you can mark my answer as correct – Tom J Nowell Feb 22 '16 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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