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
- Multiple queries and pagination
- Custom WP_Query id
- Best choice for multiple loop in page?
- Debugging wp_query orderby for taxonomy
- Is it possible to query from external database? [duplicate]
- get_template_part causes 500 error
- WP Query – order posts by meta field first and then order the rest
- Can’t sort custom post type by random
- pre_get_posts – Trying to get property of non-object warning
- Query posts that contain video?
- Get multiple users with meta value in one query and populate WP_User class
- WordPress query posts with multiple post_meta data
- How to display the featured image for each post?
- Assign custom parameter to each post in query
- How to provide meta_key array to wp_query?
- Wp_query with 2 meta keys and array of meta values
- Adding a html class based on post count
- Check for the main query from the template
- how to use pre_gets_posts to exclude one queried ID from homepage loop
- How to add div blocks after certain set of post
- Trying to retrieve random post, getting a page
- How to overwrite orderby with add_query_var
- Filter post query to only show direct children of category
- Posts in loop all show the same author when there are many authors
- Exclude recently updated post from custom WP_Query using multiple loops
- Get a list of posts by specific category
- get_posts output always same post
- Show a 404 error page if Public query variable’s value doesn’t exist?
- Exclude taxonomy term from all loops, but having it on widget
- Multiple Rows in a using wp_query
- How to use WP_Query() on single.php?
- How to check the array values, what WP_Query has brought to me?
- sort and display posts by custom field (wp-query and the loop))
- How to create custom query by keyword in post title?
- difference between querying database and using the loop
- If have posts a week old, display, else display a different loop
- WP_Query – Adding “offset” posts to the end of the loop
- Stomping WP_Query in author archive to facilitate pagination with custom queries
- How to make posts unqueryable/unpublish posts where ACF relationship field is an unpublished post? [closed]
- Conditional operator OR not working with custom fields
- How to set up hierarchical relationships without using plugins / meta query
- How to add custom meta to ‘pre_get_terms’?
- Set front_page programatically after user login via query, while leaving site option alone
- Loop increase in while loop not working
- Create a loop on my pages with new “WP_Query”
- Get posts that do not have the same tags as current
- queried_object using pre_get_posts gets notices and warning
- Utilising an existing page while using the “s” query parameter
- posts archive page – closing WP_Query loop correctly [closed]
- Create a page template for “top rated posts” but show full content and not just a list
- has_excerpt() not working inside wp_query
- Shortcode for custom query not returning results when attributes are added
- wp_query is showing posts from other categories
- Daily drip of posts – based on user ‘startData’ – ordered oldest to newest
- How to add tax_query to $args with concatenation
- How to display SQL query that ran in WC_Order_Query?
- Meta query compare for ID’s greater than specific ID
- Exclude a Woocommerce product from WP_Query
- WP ForLoop to compare meta information of posts to determine what post to display
- Yoast primary category query modification
- Query where ANDing slug values not working
- AJAX Breaking Offset Argument In WP Query
- WP_QUERY wrong ammount of posts
- How to add sort order to incremented and paginated category loop
- How does WP generate the default $query in WP_Query based on the URL?
- Continue or break the while loop
- Update query for wp_posts and wp_postmeta
- inserting content of 1 Post to in another with a template hierarchy