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 should you use WP_Query vs query_posts() vs get_posts()?
- When to use WP_query(), query_posts() and pre_get_posts
- Should I use Pre Get Posts or WP_Query
- Using pre_get_posts with WP_Query
- order by numeric value for meta value
- Wp get all the sub pages of the parent using wp query
- Return only Count from a wp_query request?
- WP_Query orderby post__in remains ineffective in the Loop [closed]
- How to query for most viewed posts and show top 5
- WP_Query vs get_posts
- What is “main query”? [duplicate]
- WP_Comment_Query pagination, delving into the unknown
- Usage of the new “posts_clauses” filter in WordPress 3.1?
- Reduce or prevent calling of update_meta_cache
- 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?
- Sort posts by category name and title
- What is the most efficient way of querying posts based on visits and date for current day?
- Order Search Results Page by meta_value If no Value Return Remaining Results
- Custom WP_Query order by post_meta and (author) user_meta
- Query WooCommerce orders where meta data does not exist
- WP_Query for WooCommerce Products
- How-to exclude terms from the main query the most performant way?
- Changing Posts Per Page and offset with pre_get_posts
- author.php with ACF and CPTs
- Slow SQL_CALC_FOUND_ROWS Query
- Query Custom Meta Value with Increment
- “pre_get_posts” firing on every query
- RSS feed with specific keyword
- When should you use wp_reset_postdata vs wp_reset_query?
- Get posts by meta data OR title
- Identify which loop you are hooking into; primary or secondary?
- Perform query with meta_value date
- Using is_main_query to select custom post type on certain page
- WP_Query ordered by custom field that is a date string?
- Multiple orderby parameters in pre_get_posts() action
- How to make an activities stream mixing posts and comments?
- Duplicate Queries
- How to query for a week using key => value WP_Query argument notation?
- WordPress Number of Posts Not Changing With posts_per_page
- Finding all results from database within 500 miles of the given latitude and longitude [closed]
- SQL Statement generated by WP_Query not producing expected results
- Show only oldest post by author
- Order posts by tags count?
- SQL query equivalent to WP User Query
- Add and in the header while looping over custom query in page template
- How to get the posts published in last two days using WP_Query?
- Highlighting Sub topic in a post?
- Sorting Posts by custom field
- How to pass custom parameter to WP_Query for filtering in pre_get_posts
- Which custom query am I in and how can I access its properties & methods?
- Query with pre_get_posts to get pagination
- How to count post type that has a particular term?
- Recommended way to drop a pending query (in pre_get_posts)?
- Single page theme that uses pages for the content
- Finding the next 5 posts
- how to retrieve specific product attribute value in an sql query?
- WordPress custom search form with pre_get_posts not work
- Dynamically Override Fancy Title – Part II
- Transient pagination not working properly
- Foreach-generated custom tax queries, each with an ajax “Load more” button
- If orderby parameter using pre_get_posts is the same for multiple posts what fallback does the query use?
- How to target the default Recent Posts and Recent Comments widgets with pre_get_posts?
- view queries made?
- order by multiple meta keys in pre_get_posts
- meta_query on a date range using an array of values
- WP_Query Performance Issues with meta_query
- WordPress custom archive page
- How to speed up wp_query, took more 5s to run against 100k posts
- How to sort posts in admin by titles with dd.mm.yyyy format?
- WP_query category__in not working, only pulls from first category
- How to detect custom query inside `posts_where` hook?
- Pass the same object to multiple widgets in a template with one query
- Is temporarily overwriting $wp_query a bad idea?
- Get the post permalink within the loop but without additional DB query
- Query Set Order By Author
- WP_Tax_Query with post_tag not working
- List the 5 most recent child pages
- Retrieve or Query Pages by ID
- How to Modify Taxonomy Archive Page with Search Parameter?
- Using pre_get_posts on a specific core/query block
- How do you query wordpress posts using a math formula between multiple meta field values?
- Searching through different categories on different pages code is not working
- How to display future posts – modified query still yields 404
- Custom Post order for homepage
- How to implement time filter to show random post 1 month for one category and 3 months for other categories