20 votes

Sort on meta value but include posts that don't have one

Easy Peasy, just tested 2018, using in production currently. Update 2022: Modified so that the NOT_EXISTS query comes before the EXISTS query, and clarifies the effect that multiple keys has on the ...
user avatar
  • 319
17 votes
Accepted

multiple orderby in pre_get_posts action

As Milo said : $query->set('meta_key', 'wpcf-object-sold-status' ); $query->set('orderby', array('meta_value' => 'ASC', 'date' => 'DESC')); // $query->set('order', 'ASC DESC' ); // not ...
user avatar
  • 607
14 votes
Accepted

Using pre_get_posts on true pages and static front pages

I finally got it working, but not with the code in my question. I totally scrapped that whole idea and restarted going in a new direction. NOTE: If anyone is ever able to sort out the issues in my ...
user avatar
11 votes

Sort on meta value but include posts that don't have one

This method will return all of the posts including those with and without the requested meta_key, but it will do weird things when ordering. add_action('pre_get_posts', 'my_stuff'); function my_stuff ...
user avatar
  • 219
11 votes

When to use WP_query(), query_posts() and pre_get_posts

I modify WordPress query from functions.php: //unfortunately, "IS_PAGE" condition doesn't work in pre_get_posts (it's WORDPRESS behaviour) //so you can use `add_filter('posts_where', ....);` OR ...
user avatar
  • 5,321
8 votes

Sort on meta value but include posts that don't have one

The problem that everyone here is having has to do with the order of the meta queries. In order to sort correctly, you will need to put the "NOT EXISTS" query before the "EXISTS" query. The reason ...
user avatar
  • 938
8 votes

When to use WP_query(), query_posts() and pre_get_posts

Just to outline some improvements to the accepted answer since WordPress evolved over the time and some things are different now (five years later): pre_get_posts is a filter, for altering any ...
user avatar
  • 4,169
8 votes

How to put custom post types on front page

Edit: This answer was written before I realised the OP has a static front page. I've left it here in case it's useful to anyone else and added a second answer for the static front page case. This ...
user avatar
7 votes
Accepted

pre_get_posts with get_posts

Firstly, you are invoking an infinite loop, which causes the memory exhaustion. To avoid it, put the following at the beginning of your function: // avoid infinite loop remove_action( 'pre_get_posts',...
user avatar
7 votes
Accepted

$query->set in pre_get_posts is unintentionally affecting the backend

In regards to your decision not to use WP_Query, pre_get_posts is actually an excellent choice to make, rather than creating a new instance of WP_Query. In fact, pre_get_posts is exactly what you ...
user avatar
  • 4,469
5 votes
Accepted

Add Custom Post Type to Current Query

You are actually not that far away with pre_get_posts, you just need to first grab the current post types that are in the query before adding your own. Also, I'd recommend using a $priority of 99 so ...
user avatar
  • 3,170
5 votes
Accepted

Changing Posts Per Page and offset with pre_get_posts

Here is an answer I recently did on another question, the same as your. Because the answer was never upvoted or accepted, I could not mark this question as duplicate, so I have deleted the answer in ...
user avatar
5 votes

order by multiple meta keys in pre_get_posts

Maybe a bit late … add_action( 'pre_get_posts', function($query) { if ( !is_admin() && $query->is_main_query() && is_post_type_archive( '[your_post_type]' ) ) { ...
user avatar
  • 166
5 votes
Accepted

How to change posts order on Category and Tag archives?

You can use pre_get_posts action to modify this order. Here's the code: function my_change_posts_order( $query ){ if ( ! is_admin() && ( is_category() || is_tag() ) && $query->...
user avatar
5 votes

RSS feed with specific keyword

Here's how we can use the builtin public query parameters on feeds: Query parameter: s We can use the s query parameter on the feed: https://example.com/feed/?s=KEYWORD This will generate a feed ...
user avatar
  • 64.4k
4 votes
Accepted

Trying to exclude first 5 posts from the first page on the homepage

offset overrides pagination, because when you get down to the query level, it's paginated via offset. You can still use offset though, you just have to do some math to multiply your desired offset by ...
user avatar
  • 76.3k
4 votes
Accepted

How to uniquely identify queries?

tl;dr: build affordable unique identifiers for queries is possible, but is pretty useless. If you're looking for unique identifiers for query objects, then you can use spl_object_hash, but IMHO is ...
user avatar
  • 44.8k
4 votes
Accepted

Sorting Posts by custom field

To expand on @pieter-goosen's comment, you do indeed want to use pre_get_posts. In your example, by using WP_Query, you're overwriting the entire query and just resetting most parts of it to default. ...
user avatar
  • 9,947
4 votes
Accepted

Modify query in pre_get_posts action is messing up my nav menu

pre_get_posts runs for every query. That includes the main query, secondary queries, and menus. The problem is that is_post_type_archive(array('provider')) is checking if the main query is the post ...
user avatar
4 votes
Accepted

Add custom field (value) to search result (without plugin)

Wordpress search in posts table. You must change the search query by adding a postmeta table (with LEFT JOIN) and then add your own conditions to WHERE clause. <?php /** * Extend WordPress ...
user avatar
  • 4,254
4 votes
Accepted

pre_get_posts with tax_query causes empty result

You use tax_query incorrectly. Take a look at Codex Page tax_query should be an array which can contain: relation - it should be string (AND/OR) taxonomy term - array with defined taxonomy, field, ...
user avatar
4 votes
Accepted

Does pre_get_posts affect REST API responses?

Yes, pre_get_posts runs for REST API requests. Your issue is likely that your request is not properly authenticated, so $user_ID is not set. To allow the REST API to recognise the logged-in user you ...
user avatar
4 votes
Accepted

How to exclude latest x posts from a paginated query?

One way is to use the offset parameter in WP_Query, but it overrides the paged parameter according to the docs. Example formula for an offset with pagination: 'posts_per_page' => $posts_per_page, ...
user avatar
  • 64.4k
3 votes

WP_Tax_Query with post_tag not working

I did get the thing working, though I'm not entirely sure how. I made two changes: Removed the "convert array to Tax_Query object" step (which I only put in because using an array wasn't working ...
user avatar
3 votes

Sticky Posts exceed posts per page limit

There an issue if the sticky posts are in the first page. The solution is to decrement the sticky post count for the sticky posts that are part of the first page. function ...
user avatar
  • 130
3 votes

Sort on meta value but include posts that don't have one

I also encountered a similar problem and the following solution helped me: $args = array( 'post_type' => 'kosh_products', 'posts_per_page' => -1, 'meta_query' => array( 'relation' => '...
user avatar
3 votes
Accepted

How to alter the query using pre_get_posts hook and is_post_type_archive

You should be using methods of the $query instance instead of the is_* template tags. And are the categories you are trying to filter a custom taxonomy? cat only works on the default category taxonomy:...
user avatar
  • 35.7k
3 votes
Accepted

Another query in pre_get_post cause memory issue

Remove the action before you run the internal query, and always verify main query with is_main_query: function custom_loops($query) { if ( $query->is_tax( 'service_photo_location' ) && ...
user avatar
  • 76.3k
3 votes
Accepted

Date Query to Pull Current and Future Posts

I wonder if you mean this kind of date_query: $query->set( 'date_query', [ [ 'after' => 'today midnight', 'column' => 'post_date_gmt', 'inclusive' => ...
user avatar
  • 64.4k
3 votes
Accepted

How to add taxonomy filter on the query fly?

You have a syntax-ish error, or a spelling one. There is a space after "taxonomy" in your tax_query array. If that were the sum of it I'd have just posted a comment, but additionally that $query->...
user avatar
  • 63.7k

Only top scored, non community-wiki answers of a minimum length are eligible