Just a quick update that a new method is_main_query()
has been introduced in WP 3.3.
Example:
add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
function foo_modify_query_exclude_category( $query ) {
if ( $query->is_main_query() && ! $query->get( 'cat' ) )
$query->set( 'cat', '-5' );
}
Resources:
- WPDevel: New API in 3.3: is_main_query()
- Codex reference: is_main_query()
- Related Trac Ticket #18677
Related Posts:
- WP_Query vs get_posts
- Usage of the new “posts_clauses” filter in WordPress 3.1?
- WP_Query: query posts by ids from array?
- pre_get_posts with get_posts
- Query Custom Meta Value with Increment
- Transient pagination not working properly
- meta_query on a date range using an array of values
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- How to change posts order on Category and Tag archives?
- Undefined WP_Query::has_posts()?
- Create ONE callback for all page templates, post filter queries + paginated pages, triggering pagination via AJAX
- if/else on custom query gives 200 OK when condition not met?
- How to use the query hook/filter?
- Set global $wp_query/$post variable for dynamic page generation
- Order by empty custom field
- Check if loop has any categories?
- How to get list of posts from permalinks?
- Is_single() conditional tag returns null in query
- Transient api Caches confused
- How can I override one post and make it display content for another post?
- Include current post into loop
- Should I reset $wp_query?
- How to filter query loop block with a search string from the query parameters
- I am officially missing something about transient posts
- WP_Query & Duplicate entries
- how to show more than 1 post into three columns query
- Ordering Posts by parent category, name ascending
- WP_Query secondary query failing
- Loop through multiple custom fields with increasing number
- meta_query dates from an array
- Main loop querying current template’s info only in custom category archive pages, not my posts
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise
- Resetting post data to previous loop in nested loops
- Should we trust the post globals?
- Is it possible to completely stop WP_Query retrieving posts?
- Pagination returns 404 after page 20
- Make a WP Query search match exactly the search term
- Make loop display posts by alphabetical order
- get custom post type by tag
- Finding the next 5 posts
- Trying to list out child pages with WP_Query
- Pass the same object to multiple widgets in a template with one query
- Why execute the_post()?
- 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?
- WP_Query on custom field and order results
- How to query a custom post type with a taxonomy filter but display post type archive page?
- How to display 7 most recent days of posts?
- Create A Loop With A Variable Number of Posts For Each Bootstrap Row?
- Change query_posts to WP_Query in page but does not work
- Where is the WP_Query instance instantiated (when working with themes)?
- Optional Meta Query
- Set conditional on template based on referring page slug
- Filter posts in category archive page by year using a dropdown
- Show post number in widget loop
- Search form not working with custom query?
- Include data from custom table in WP_Query
- WordPress ignoring LIMIT / posts_per_page despite being in wp_query
- Conflict with get_posts and the_content
- date_query seems to be ignored by wp_query
- Better wordpress attachment query than this
- How do I rewrite this loop as a new WP_Query style-loop?
- Is it possible to query from external database? [duplicate]
- Can’t sort custom post type by random
- Exclude taxonomy term from all loops, but having it on widget
- How to set up hierarchical relationships without using plugins / meta query
- Meta query compare for ID’s greater than specific ID
- wordpress udpate query not equal to
- Ordering posts by custom field and grouped by month
- relation OR instead of AND – Filtered term ID’s in loop
- Performance concerns: index.php vs taxonomy-$taxonomy.php
- Why does apply_filters behave different inside and outside a loop?
- meta_query where the meta value is not the post title
- Elementor posts custom query based on tag [closed]
- How can I do a orderby by the number of items? So basically list by starting with the array with the largest number of post
- Multi-layered WP_Query
- `offset` WP_Query argument dont work via `pre_get_posts`
- Function the_posts_pagination() not compatible with WP_Query arguments
- Pagination of custom page with custom fields query
- Woocommerce Get Orders By Meta Value
- How to show specify category template for both parent and child category
- Pagination inside the blog page not working
- Custom Query Pagination not working on static front page
- Meta query doesn’t remove placeholder escape before query
- query all posts published by certain user id
- modifying the loop multiple times with arguments passed through ajax to wp_query
- WP_Query causing links to not work
- Retrieving category pages from subcategory returns empty sets
- How to get query results for the next page
- Iterate through ID’s in loop
- Different number of posts showing in development vs production server
- 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]
- Pull Instagram images into an existing loop?
- Multiple loop for “featured” items returns wrong posts
- Get WP_Query query after execution?
- First post outside of loop, homepage only?
- Search results stuck on page 1
- Pagination only showed when no category is set in wp_query