22 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 ...
noahmason's user avatar
  • 347
9 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 ...
Paul's user avatar
  • 958
9 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->...
Krzysiek Dróżdż's user avatar
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 ...
prosti's user avatar
  • 4,283
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 ...
Andy Macaulay-Brook's 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 ...
Tim Malone's user avatar
  • 4,734
7 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 ...
Jacob Peattie's user avatar
6 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]' ) ) { ...
s t's user avatar
  • 176
6 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 ...
birgire's user avatar
  • 67.1k
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 ...
nmr's user avatar
  • 4,501
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, ...
Krzysiek Dróżdż's 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 ...
Jacob Peattie's 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, ...
birgire's user avatar
  • 67.1k
3 votes
Accepted

Using meta_query and custom fields within pre_get_posts to return posts within a numerical range

The type argument in your meta query is wrong. It spells NUMERICAL but the correct value is NUMERIC. $meta_query = array( array( 'key' => 'bpm', 'value' => array($...
cybmeta's user avatar
  • 20.5k
3 votes
Accepted

Custom query filter not working on woocommerce category page

I'm not sure at first glance why this is only working on your home page and not your archive pages, but there are a few things wrong with the way this function is written. So, I've rewritten it for ...
Tim Malone's user avatar
  • 4,734
3 votes

pre_get_posts variables

The logic inside the is_home function relies on the main query, but by definition that query hasn't happened yet as you're still in the pre_get_posts filter. Instead, consider $q->is_home(). The ...
Tom J Nowell's user avatar
  • 59.2k
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' => '...
Ilya Kogan's user avatar
3 votes

Sticky Posts exceed posts per page limit

The answers written here get the job done, but WordPress provides a simpler way. As part of the WP_Query class, WordPress includes the current_post property. This property increases by one every time ...
jg314's user avatar
  • 501
3 votes
Accepted

How pre_get_posts filter by roles in WP Admin

I'd make 2 queries. This would look something like this : function add_role_filter_to_posts_administration(){ //execute only on the 'post' content type global $post_type; if($post_type ==...
JMau's user avatar
  • 2,913
3 votes
Accepted

How to put custom post types on front page

So if you've registered a CPT called wpse_242473_custom_post_type you can use this to put 6 recent posts of that type onto your static front page (or anywhere). You can use a shortcode or a template ...
Andy Macaulay-Brook's user avatar
3 votes
Accepted

Why the 'date_query' is not working in 'pre_get_posts' hook?

You've got trailing space in your $query->set(). Instead of $query->set( 'date_query ', ... it should be: $query->set( 'date_query', ...
Paul 'Sparrow Hawk' Biron's user avatar
3 votes
Accepted

Using more than one meta_key in pre_posts_query

Use WP_Query to select any post based on meta key and value. You can also sort posts Ex: $args = array( 'post_type' => 'events', 'orderby' => 'meta_value_num', //probably you will ...
Yamona's user avatar
  • 278
3 votes
Accepted

Shouldn't I be able to modify the main query by this filter?

The reason your code isn't working is because by the time archive.php runs, the pre_get_posts hook has already fired. You can easily accomplish the task in your functions.php file: add_action( '...
Nathan Johnson's user avatar
3 votes
Accepted

How to override a query and display specific page by ID?

You could do this a few different ways, depending on what's happening in the template and how you are able to identify which page you want to override. 1. Simple method with pre_get_posts: This will ...
Milo's user avatar
  • 78.5k
3 votes
Accepted

How to sort posts in admin by titles with dd.mm.yyyy format?

It looks like the post titles have the date format DD.MM.YYYY. We can order the DD.MM.YYYY titles as YYYYMMDD with: $query = new WP_Query( [ 'orderby' => 'wpse_mod_title ' ] ); that's supported ...
birgire's user avatar
  • 67.1k
3 votes
Accepted

Exclude category by slug in pre_get_posts?

Here's one way with the following tax query: 'tax_query' => [ [ 'taxonomy' => 'category', 'field' => 'slug', 'terms' => [ 'foo' ], 'operator' =&...
birgire's user avatar
  • 67.1k
3 votes
Accepted

pre_get_posts Remove tax_query Completely

This fixed it! :) Hat-tip gmazzap for the useful info: Obliterate the main query and replace it function wpse_286813_omit_all( $query_vars ){ // triggered also in admin pages if ( is_admin() ) ...
admcfajn's user avatar
  • 1,316
3 votes

How to exclude pages from WP search based on page title

You can query a list of post IDs using $wpdb based on their slugs, and then use that in your filter. Here's a simple example of how to do this: function mm_search_filter( $query ) { // We will ...
Johansson's user avatar
  • 15.3k
3 votes
Accepted

is_category in pre_get_posts results in php notices

is_category('category-name') won't work until after the query is run. If we check source code where those notices are generated, we can see that it's using get_queried_object, which gets populated ...
Milo's user avatar
  • 78.5k
3 votes
Accepted

Automatically applying a pre_get_posts filter for child categories only

Try this: EDIT: added short circuit return statements to avoid running unnecessary code on admin pages or other queries (e.g. nav menu items) // Custom query for research sub-categories function ...
mrben522's user avatar
  • 1,673

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