I am reading that query_posts()
should be avoided in favor of wp_query()
and pre_get_posts()
. I am not confident with messing with the Loop and do not fully understand the codex.
Does the code below use query_posts()
? If yes and since query_posts()
should be avoided, can you suggest a method that does not use query_posts()
but still accomplish the same thing?
This code in functions.php
is used to sort posts by random or by price.
function my_custom_query($query){
if ( $query->is_home() && $query->is_main_query() ) {
$sort= $_GET['sort'];
if($sort == "pricelow"){
$query->set( 'meta_key', 'price' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
}
if($sort == "random"){
$query->set( 'orderby', 'rand' );
}
}
}
add_action( 'pre_get_posts', 'my_custom_query' );
.
Link A (Random) and Link B (Price) are posted in my menu by using this code. Thus the visitor to the website can sort the posts simply by clicking a link.
<a href="http://website.com/?sort=A">Random</a>
<a href="http://website.com/?sort=B">Price</a>
Creating a new WP_Query() object is always fine.No no no sorry about that. I didn't see the pre_get_posts hook.
The code in your question is good for hooking queries. As in described in WordPress Plugin API/Action Reference/pre_get_posts:
So it hooks the default WP_Query() where you want (in your code, it changes WP_Query on GET request).
In your template files, use new WP_Query($args).
I have done a very detailed explanation on this very topic on WPSE, and for the sake of the value and benefit it might have for SO users, here is the complete post copied from that question on WPSE. For interest sake, here is a link to the complete post on WPSE: Some doubts about how the main query and the custom query works in this custom theme?
Your actual question is basically when to run a custom query and when to make use of the main query. Lets break it down in three parts
PART ONE
When to run a custom query (This is not a definitive list)
To create custom content sliders
To create a featured content area in a page
On page.php templates if you need to display posts
If you require custom content on a static front page
Display related, popular or informational posts
Any other secondary or supplementary content outside the scope of the main query
When to make use of the main query.
To display the primary content on
On your homepage and the page set as a blogpage in the backend
All archive pages which includes templates like archive.php, category.php, author.php, taxonomy.php, tag.php and date.php
PART TWO
Correct. This falls out of scope for the main query. This is secondary or supplementary content which cannot be created with the main query. You SHOULD ALWAYS use either
WP_Query
orget_posts
to create your custom queries.NEVER USE
query_posts
to create custom queries, or even any other query. My emphasis.Moving on
That is all wrong and your statement is unfortunately true. As said before, NEVER use
query_posts
. It runs a complete new query, which is bad for performance, and it most cases breaks pagination which is an integral part of the main query for pagination to work correctly.This is your primary content, so you should be using the main query with the default loop, which should look like this, and this is all you need
You can completely get rid of this part, delete it, burn it and forget about it
OK, once you've done that, you'll see that posts from the feature tag appear in your home page using the main query and default loop.
The correct way of removing this tag from the homepage is with
pre_get_posts
. This is the proper way to alter the main query and the hook you should always use to make changes to your primary content loop.So, the code with
pre_get_posts
is correct and this is the function that you should use. Just one thing, always do a check that you are not on an admin page becausepre_get_posts
alters the back end as well. So this is the proper code to use in functions.php to remove posts tagged featured from the homepagePART THREE
Extra reading material which will be helpful in future
Conditional tags
When should you use WP_Query vs query_posts() vs get_posts()?
When to use WP_query(), query_posts() and pre_get_posts
Query Overview
Guidance with The Loop for CMS