pre_get_posts
filters run before WordPress has figured out the main query, which is used to pick the template. For this reason you can’t put it inside the template, it has to run before the template is picked, not afterwards.
For this reason, it has to go inside your themes function.php
or in a plugin
Related Posts:
- Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
- How to uniquely identify queries?
- pre_get_posts with get_posts
- Can not switch the queried post in pre_get_posts hook
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- Using WP_Query in “parse_query” or “pre_get_post” in Posts2Posts
- pre_get_posts – Trying to get property of non-object warning
- `offset` WP_Query argument dont work via `pre_get_posts`
- pre_user_query vs pre_get_posts
- Using the same WP_Query for shop and widgets in WooCommerce shop
- wp_query – Modify $query to include duplicate content
- Custom post types loop on a page template
- Search query alteration not working for meta values
- When to use WP_query(), query_posts() and pre_get_posts
- Should I use Pre Get Posts or WP_Query
- Return only Count from a wp_query request?
- What is “main query”? [duplicate]
- How to add taxonomy filter on the query fly?
- Make a WP Query search match exactly the search term
- WP_Query: query posts by ids from array?
- Order Search Results Page by meta_value If no Value Return Remaining Results
- Query WooCommerce orders where meta data does not exist
- Query Custom Meta Value with Increment
- RSS feed with specific keyword
- Multiple orderby parameters in pre_get_posts() action
- Finding all results from database within 500 miles of the given latitude and longitude [closed]
- Show only oldest post by author
- How to get the posts published in last two days using WP_Query?
- Query with pre_get_posts to get pagination
- Finding the next 5 posts
- Dynamically Override Fancy Title – Part II
- Transient pagination not working properly
- If orderby parameter using pre_get_posts is the same for multiple posts what fallback does the query use?
- Get the post permalink within the loop but without additional DB query
- List the 5 most recent child pages
- pre_get_posts filter meta_query without conflicting existing meta_query
- What is considered a default query for the request filter?
- $wpdb insert is not work
- How to find exact match for search term in WP_Query? What is the additional string added in LIKE query in WP_Query?
- My Main Query Modification is Messing up my dynamic main – why?
- Query total number of posts
- Creating a custom search for a specific post type
- How to query a custom post type with a taxonomy filter but display post type archive page?
- Custom query for sidebar isn’t returning results
- Change query_posts to WP_Query in page but does not work
- Optional Meta Query
- Set conditional on template based on referring page slug
- Filter posts in category archive page by year using a dropdown
- Query posts intersecting tags and categories
- WP_Query ignoring tax_query when is_singular
- Applying posts_clauses filter to specific queries only
- How to get a query variable?
- date_query seems to be ignored by wp_query
- Query Page Content From Theme Options?
- Better wordpress attachment query than this
- Modify Search Query if original Query gave no results
- Include current post into loop
- Is it possible to query from external database? [duplicate]
- Can’t sort custom post type by random
- Filter post query to only show direct children of category
- pre_get_posts – editing query, tax_query
- Is it possible to retrieve posts depending on meta_key LIKE condition? [duplicate]
- WP_User_Query orderby meta_val_num
- WP_Query custom order and pagination
- Unable to paginate a custom page query
- meta_query where the meta value is not the post title
- Multi-layered WP_Query
- Pagination of custom page with custom fields query
- Query pages created by deleted users
- Woocommerce Get Orders By Meta Value
- meta_query is overriding default search
- Exclude post from wp_query based on custom field boolean
- Get posts that match defined arrays of tags
- Move some posts to end of sort order, even if there is a sort in the wp_Query already
- Order posts by tags count?
- Strange results from WP_Query
- Is there a way to control both Order By and Order query parameters from one input field
- Pre get posts sort by meta key returns no results if meta key does not exist
- I need query_posts() to order results first by a meta value and then by post ID
- Pre_get_posts only show posts by administrator roles
- Modify a query with no results in pre_get_posts
- return child post if available otherwise parent post
- Custom Order Current Query: By Meta Key and Category
- Search has query that will return no results
- WP Query – Search in title or author_name
- URL to Post Archive of Custom Query
- Meta query doesn’t remove placeholder escape before query
- orderby in WP_QUERY – Use the order from the Dashboard
- Random posts in WP_Query when searching by tag
- Ordering Posts by parent category, name ascending
- WP_Query always returning the last custom post
- How to get query results for the next page
- How to query post into a complex slider
- Order posts by custom column using pre_get_posts
- Combine privileged users array with my current query
- Horizontally paginate through sets of blog posts?
- How to organize a WP_Query’s list of posts by category and display category title?
- Need help setting up a search form for wordpress [duplicate]
- meta_query dates from an array
- How to get posts that have certain meta key value and order based on another meta key’s value