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.

I have read other answers, but I don't understand which is more fast (the best and simple for slow server running)?

I have a website with over 1 000 000 hits /day and a slow server with limitations at database connections, so I want to optimise my site.

What do you recommend me to use: WP_Query or query_posts() or get_posts()?

And if I want to show articles in different positions, with different styles can I repeat the function [WP_Query or query_posts() or get_posts()] on every article that I show, or it is best to use it one time and use if $i == 1,2,3 for different styles?

share|improve this question
1  

2 Answers 2

up vote 1 down vote accepted

This answer explains the differences. They all use WP_Query, all parameters being equal there is no difference in speed, it's just a question of which is easier for you for your particular needs (hint: it will never be query_posts, which should basically never be used).

You should query the database as few times as possible, so if you can fetch all your posts with a single query, then do some manipulation via php to get the output you need, that is definitely what you should do versus creating multiple queries. Note that a WP_Query object contains some helpful vars to assist in output, like $query_object->post_count and $query_object->current_post.

You should also be employing a cacheing plugin, like W3TC, if server load is an issue.

share|improve this answer

There's really no need to use query_posts() AT ALL, ever, for any situation.

Why you may be asking?

Simple. Because it instantiates a new WP_Query object and assigns it to the global variable wp_query. And this also explains why using this function jacks with everything else and why you must reset stuff after using it.

Here is the actual query_posts() function

function query_posts($query) {
   $GLOBALS['wp_query'] = new WP_Query();
   return $GLOBALS['wp_query']->query($query);
}

The solution => Just get really good at using the WP_Query object. Practice makes perfect.

If you really want to master this thing, go to wp_includes/query.php file in the WordPress core and just start reading it until you understand the logic of it. It may take a bit, but once you "get" it. Then you'll have it forever.

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.