Basically what you are looking for is the global $wp_the_query
variable which is set to the value of the main query. It may not be a perfect fit for 100% of cases but will probably work fine in 99% of cases:
add_action( 'pre_get_posts', 'custom_post_count' );
function custom_post_count( $query ){
global $wp_the_query;
if ( $wp_the_query === $query ) {
$query->set( 'posts_per_page', 5 );
}
return $query;
};
Related Posts:
- pre_get_posts with get_posts
- How-to exclude terms from the main query the most performant way?
- 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?
- Show Sticky Post at the top but do not show again in the loop?
- Can I set my WP_Query to be a Main Query?
- Show scheduled posts in main loop but not in WP_Query?
- WP Query – order posts by meta field first and then order the rest
- Can I alter the main loop to ‘orderby’ a custom callback?
- When to use WP_query(), query_posts() and pre_get_posts
- Should I use Pre Get Posts or WP_Query
- Can I force WP_Query to return no results?
- Using pre_get_posts with WP_Query
- Resetting post data to previous loop in nested loops
- Some doubts about how the main query and the custom query works in this custom theme?
- Get post count of current loop when using multiple queries on one page
- Multiple WP_Query loops with Pagination
- Using a custom WP_Query with get_template_part loop
- WP_Query orderby post__in remains ineffective in the Loop [closed]
- WP_Query vs get_posts
- What is “main query”? [duplicate]
- Loop within a loop?
- WP_Query and next_posts_link
- Show posts without term
- Usage of the new “posts_clauses” filter in WordPress 3.1?
- Display products from specific category in shop page
- How to list some posts first in the loop based on post id
- Reduce or prevent calling of update_meta_cache
- How to add taxonomy filter on the query fly?
- Pagination returns 404 after page 20
- Make loop display posts by alphabetical order
- WP_Query: query posts by ids from array?
- Use WP_Query with have_posts()?
- Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
- get custom post type by tag
- How to get order of posts?
- Order posts by ID in the given order
- How to uniquely identify queries?
- Order Search Results Page by meta_value If no Value Return Remaining Results
- Too slow when using both ‘tax_query’ and ‘meta_query’ both in WP_Query
- get_template_part in for loop
- Get array of posts from the current archive page loop
- Pagination with WP_Query is buggy – working for some pages, but not the others
- Get the number of posts from the current page results
- Changing Posts Per Page and offset with pre_get_posts
- How to exclude latest x posts from a paginated query?
- Executing Queries in tag.php
- Display posts the match taxonomy term linked from wp_list_categoies?
- How can I save an array from a random post sequence for later use?
- How to place a loop within another loop?
- WordPress Custom Query to show posts from last x years
- How to order posts tag by tag?
- Slow SQL_CALC_FOUND_ROWS Query
- Query Custom Meta Value with Increment
- Show two random posts from custom post type
- RSS feed with specific keyword
- Add the “active” class only to the first loop item in a WordPress query [closed]
- Loop through all tags & output posts in alphabetical list
- Pagination with 5 posts per page
- Identify which loop you are hooking into; primary or secondary?
- Using is_main_query to select custom post type on certain page
- In loop: posts have thumbnail AND other variables
- Multiple orderby parameters in pre_get_posts() action
- Group posts by custom field
- WordPress Number of Posts Not Changing With posts_per_page
- How to order category.php loop by ‘meta_value’?
- SQL Statement generated by WP_Query not producing expected results
- Pagination/infinite scroll with WP_Query and multiple loops
- Merging a complex query with post_rewind and splitting posts into two columns
- Sort posts by custom taxonomy name
- Add inline HTML to posts published within last 24hrs
- Move posts to top of WP_Query if in certain Taxonomy?
- Sorting Posts by custom field
- How to pass custom parameter to WP_Query for filtering in pre_get_posts
- Loop inside the loop
- Query with pre_get_posts to get pagination
- Pagination Not working on Home Page with 2 Query
- Recommended way to drop a pending query (in pre_get_posts)?
- meta_query not working properly
- Ajax Infinite Scroll In Custom WP_Query Loop Not Working
- Single page theme that uses pages for the content
- Display different number of posts from one category on the different pages
- Looping through tabular data
- Trying to list out child pages with WP_Query
- Exclude post on loop by multiple meta key value
- Pagination not working Search posts
- How to change a custom query into a standard loop?
- WordPress custom search form with pre_get_posts not work
- Transient pagination not working properly
- Show default content if custom WP_Query has no posts
- get_posts() seemingly ignoring post_type
- If orderby parameter using pre_get_posts is the same for multiple posts what fallback does the query use?
- cloning a WP_Query
- Can not switch the queried post in pre_get_posts hook
- Injecting content with $wp_query->current_post restarts from zero on paged pages. How to inject content after X posts, regardless of pagination?
- How to query ‘posts_per_page’ to display a different blog posts index template?
- How to target the default Recent Posts and Recent Comments widgets with pre_get_posts?
- Display featured products through custom loop in WooCommerce 3 [closed]
- Woocommerce custom loop to show all the products [closed]
- Sort by meta key on archive page