pre_get_posts
triggers for every query (front and admin side). It also triggers when querying nav-menu items (which is what is happening here).
Most of the time you only want to filter particular queries – so you need to check if that query is one you wish to modify.
Usually – and I think your example might be one – you only want to modify the ‘main query’.
function wpse72969_pre_get_posts( $query) {
//If its not the 'main query' don't do antying.
if( !$query->is_main_query() )
return;
return $query;
}
add_action('pre_get_posts', 'wpse72969_pre_get_posts');
More generally all the conditionals are available to you (Note: you want want to use the methods and not the functions: e.g. $query->is_search()
as opposed to is_search()
.
You can even set custom variables to identify queries.
Related Posts:
- Using meta_query on pre_get_posts to exclude a particular meta_key value
- WordPress Screwing Up ‘orderby’ => ‘meta_value_num’ in ‘pre_get_posts’
- Can I use pre_get_posts to add up values from two different meta_keys?
- meta_query Array
- Order resultset by configured value and then list all the rest
- WordPress meta query not working
- Why does this incorrect pre_get_posts meta_query work for sort order?
- Admin products page column not sorting
- How to do a meta query using REST-API in WordPress 4.7+?
- Meta query terribly slow
- meta_query sorting by 2 keys
- Get result from meta_query() between two numbers
- How to handle optional end date in compare clause in meta_query
- Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
- Query between two meta keys
- Archives for custom post type based on a “date” meta value
- How to check a single number value against an array of meta_key values?
- get_users meta_query
- Meta query with multiple logic (AND / OR)
- Best practice – Meta Query vs. post_clauses for “left join” ordering
- Meta query interfering with orderby relevance
- meta_query with multiple values
- Different page parameters inside query
- Return posts with specific meta key first, but the rest in alpha order by last name
- meta_query key value from array
- filter search result with custom post type meta key
- LIKE %…% Meta Query
- Multiple Values stored as array in Meta Query
- Meta Query “IN” doesn’t work with ACF checkbox filter
- Order by nested named meta queries with ‘exists’ and ‘not exists’
- Documentation for post meta
- Help With issue on pre_get_posts filter in taxonomy
- Advanced orderby with multiple meta_query
- Arrange Posts By Date In Order Of Closest To The Current Date
- Using get_terms() with meta_query parameters
- WP_User_Query with meta_query array relation ‘OR’
- Can’t sort order of wp_query with 2 meta keys
- pre_get_posts filter using numeric meta_query comparison (from dates)
- meta_query problem
- Custom Upcoming Events List for Events Manager Plugin
- Very slow query with meta_query on large database
- How to use meta_query to check if a meta field has something set?
- Using meta_query and custom fields within pre_get_posts to return posts within a numerical range
- Comparing arrays with meta_query in pre_get_posts
- pre_get_posts and search query for admin
- Using database meta_values to calculate new post order using pre_get_posts or a ‘request’ hook
- How to exlude posts that have certain meta_value?
- User Relationship
- meta_query results not the same with and without spaces
- meta_query all posts between to ages
- How to make a WP REST API query with meta_query in WP4.7?
- pre_get_posts filter meta_query without conflicting existing meta_query
- WordPress Core – Optimizing meta_query generated SQL?
- Edit post_content on posts with Featured Image
- Nested Queries using meta_query
- Display no post when metavalue is 0
- Use both meta query and tax query
- Wildcard with LIKE operator in Meta query
- meta_query, number comparison, not quite working as it should
- Meta Query Compare with Date
- Query posts only if meta_value is not empty
- pre_get_posts order by not working
- Does meta_query working with value paramater higher then 999?
- Single meta_query query using OR instead of AND in request’s WHERE statement
- meta_query keys and sticky posts
- How to pass user meta_key and meta_value (values as array)
- WP_Query ordered by meta_value_num doesn’t fetch posts without this meta field
- Query Multiple values for same key
- Custom post type archive with rand and meta_value
- Comparing a field with several values at once with meta_query
- Using meta_query with multiple keys and compare values
- Meta query with a sub query group possible?
- meta_query compare=’!=’ with multiple custom fields
- Query posts with numeric meta values within a given range
- Fastest way to do meta query when I don’t need the actual posts, and just need post_id?
- meta_query not working on live site
- How to query post only with different excerpt?
- How do you get Posts by multiple meta_keys and meta_values with the Rest API V2?
- Query custom field with date
- Meta query with regular expression does not work
- Searching in title or meta, with a tax query too!
- WP_Query with meta_query no results
- Documentation for post meta
- Exclude posts from main loop based on meta value
- Help ordering Post loop by two meta values
- Order ascending is ignored in meta query?
- WP User Query Issues
- How to use meta query for greater than value in array?
- Sanitize Custom Field Value in Meta Query
- Wp Query custom search by meta query
- Pre Get Posts / Multiple Meta Keys / Orderby Single Key
- Filter posts by meta query?
- Help on multiple meta_query
- Meta query – How to display a null meta key
- How to query wp_postmeta table for any specific meta_value?
- User meta query results in PHP notice: only variables should be passed by reference
- meta_query with multiple values optimization
- Meta query date column add [x] days to validate
- Get a single post ID based on an exact match of 1 or more meta values
- How to use orderby with meta_query?