How to apply pre_get_posts to a custom query?

For some use cases, couldn’t you use $query->get() to target specific custom queries? For example: say you’ve got a custom query for a slider post-type. Perhaps something like this? function wpse64950_filter_pre_get_posts( $query ) { if ( ! is_main_query() ) { if ( ‘slider’ == $query->get( ‘post-type’ ) ) { // This is a query of … Read more

pre_get_posts and BBPress in Swagger Theme

I’m not 100% clear on what the problem is, but I would start with putting the conditions inside the function instead of wrapping the entire function. add_filter(‘pre_get_posts’, ‘query_post_type’); function query_post_type($query) { if (!is_admin()){ global $oswcPostTypes; if(empty( get_query_var(‘suppress_filters’ ) ) { $post_type = get_query_var(‘post_type’); //get theme options global $oswc_reviews; if($post_type ) { $post_type = $post_type; set_query_var(‘post_type’,$post_type); … Read more

Exclude page by title for non admins

Get the page by its title, then use its ID: add_action(‘pre_get_posts’, ‘exclude_pages’); function exclude_pages($query) { if(! is_admin()) return $query; global $pagenow; if (‘edit.php’ === $pagenow && (get_query_var(‘post_type’) && ‘page’ === get_query_var(‘post_type’))) { // Get the page by its title, … $page = get_page_by_title(‘TITLE_HERE’); // then use its ID $query->set(‘post__not_in’, array($page->ID)); } return $query; } References: … Read more

Exclude post type with pre_get_posts?

I found a solution to this myself. It returns the default post type posts instead of excluding the custom post type which is fine for my needs. function my_breakfast_query ( $query ) { // not an admin page and is the main query if (!is_admin() && $query->is_main_query()){ if (is_tax( ‘food’, ‘breakfast’ )){ $tax_query = array( … Read more

pre_get_posts works in post type archive but not in single post

The next_post_link() and previous_post_link() functions do not use the same query that ‘pre_get_posts’ affects. They use their own custom queries. You have to use other filters to affect them. Utimately, the get_adjacent_post() function does the bulk of the work for the next_post_link() and previous_post_link() functions. Here is part of that function: $adjacent = $previous ? … Read more

pre_get_posts post_meta event

If you have store date different then timestamp it will be VERY hard to do this. To get this if is timestamp stored, you need add “meta_query” like this: add_action(‘pre_get_posts’, ‘my_pre_get_posts’); function my_pre_get_posts($query) { $offest = 0;// you must count this to avoid today and tomorrow $query->set( ‘meta_query’, array( ‘relation’ => ‘AND’, array( ‘key’ => … Read more

Pre Get Posts – Difference between conditional() and $query->conditional()?

There’s a big difference… most of the times. is_home(), just like any other conditional tag, check main query. $query->condition() check the query object that is passed by action, that can be a a completely different query, because pre_get_posts is called for all queries. As example, let’s assume you are viewing a singular post, and inside … Read more