The pre_get_posts hook can be used to modify queries before they’re run. You can use $query->set() to set arguments on the WP_Query object, and $query->is_main_query() in the callback to limit your changes to the main query:
add_action(
'pre_get_posts',
function( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 12 );
}
}
);
You can’t target specific templates, but if you want the change to only affect archives, you can use $query->is_archive(), which will be true for date, taxonomy and post type archives, or if you only want to apply the changes to the category archives, you can use $query->is_category(). Many of the normal conditional functions are available as methods for checking the current query.
Related Posts:
- How to display custom post types AND regular posts separately on a shared taxonomy archive?
- How to get the custom post type from an archive page?
- How do I display two separate taxonomy archives for two post types that share a single taxonomy?
- Loading custom page template via plugin
- Page is defaulting to archive page and not designated template
- Custom Post Type Archive URL takes over page URL
- where can I see my custom post type archive template?
- Pagination Issue: Custom Post Type In Index
- WordPress custom post type archive with description
- CPT: archive-cpt.php VS custom page template
- Help with CPT template pagination
- How to show the archive / post type description on Single templates
- is_main_query() not working for WP REST API
- Custom template page with custom archives listing by user
- Filtering WP_Query
- Create template for taxonomy results limited by Custom Post Type
- Isotope Filtering with Bootstrap Tabs – Custom Post Type Query Loop in each Tab (Have to click twice to filter)
- Why is my site using index.php instead of archive?
- Custom post type archive page blank
- How can I get this request to use the Custom Post Type page template instead?
- Archive for a Taxonomy of a Custom Post type
- how to remove pages loading with the archive templates
- How can I use archive-{post_type}.php theme template?
- Pagination doesn’t function properly for archive of a custom post type set as the front page
- Custom Post Type setup
- Custom Post Type Archive Pagination
- Archive Template being used instead of Category Template for Custom Post Type
- How can I allow users to edit text that will be displayed on a custom post type archive page?
- Is possible register two archive pages for single custom post type?
- Tell wordpress to show a single page instead of an archive page
- Turning archive templates to a page template maintaining the slug
- One of my headings is mysteriously coming up as a link in my CPT Archive
- Custom post type with two templates
- Custom Post Type tag taxonomy “Page Not Found”
- How can I pull information from my loop and divide them seperately?
- Custom post type pagination, single page issue
- Listing all custom post types using a specific term on the said term’s template page, in groups
- WP Query + custom fields: How to query event posts from the current date backwards 6 months and organize it month by month?
- WordPress CPT archive page display sticky post first and then display the rest of the posts in same Query
- Display all posts in a custom post type, grouped by a custom taxonomy
- Query by post title
- Get post with multiple meta keys and value
- Get posts for custom post type with WP_Query
- How to add custom content template part for a custom post type on main query using a plugin
- WP_Query() show posts that end later than today
- How to check if a WP_Query has data
- Sorting a query by custom field date
- Number of pages – multiple (custom) post types
- Enforcing canonical URLs with multiple custom post types
- Multiple Paginations on one Page
- How to quickly switch custom post type singular template?
- WP_Query -> sort results by relevance (= most tags / taxonomy terms in common)
- Get latest 4 post on a custom post filtered by category
- Using Custom Templates for Custom Post Types for the Genesis Theme Framework?
- WordPress custom post type category pagination 404 Error
- how to group custom post type posts by custom taxonomy terms
- Show Custom Post Type by Author
- How to sort CPT by custom meta value (date), and return posts month by month
- Custom post type archive with pagination?
- Wp-query causing problems with the_content();
- wp_query and comment_parent – select only posts with top level comments
- Comparing timestamps in meta query doesn’t work
- Include both default and Custom Post Type in query modified inside pre_get_posts
- Multiple Archive Pages for Custom Post Types AND Taxonomies
- List all custom post type posts from a given category?
- Exclude current post when getting related post on custom post type and taxonomy
- Display Posts of a Category in Alphabetical Order (Custom Post Type)
- Turn query string to sub page
- WordPress Template Hierarchy
- Custom Post Type with static page for archive
- Filter posts with meta_query NOT IN where value has multiple values
- How to query different post types in specific order?
- Loop on front-page.php
- Second Custom Post Type Archive
- Rewrite URL for only archive page (custom post type)
- Archive slider for CPT
- Querying Term Posts in Loop
- Custom Post Type Taxonomies -Posts not showing in Category or Tag pages
- Can’t get order_by meta_value_num to work properly
- Archive template for custom post type only lists first 10
- Taxonomy Archive: Display only one post per term from separate custom taxonomy
- Loop through Custom Post Type, and then show children within each iteration
- orderby in custom WP Query does not work
- Unable to retrieve any posts of CPT in wp-admin
- Set up Custom-Post-Type Author Archive
- Linking to the most recent post in a Custom Post Type
- How to make sure content doesn’t display if selection is empty
- Archive for custom taxonomy lists all posts instead of current taxonomy
- Creating a navigation menu of all posts of a custom post type and their children posts?
- Wp_query: sort by PHP variable
- WP_Query most viewed posts, in multiple Post Types, last 30 days, excluding a specific taxonomy term
- Crafting WP_Query array, sort by date
- forming WP_Query for posts of all post types but from specific categories
- How can I get the number of custom post type posts that have a specific attachment image set?
- Including metaboxes from custom post types in global search — continued
- Custom Widget WP_Query problem
- WordPress Doesn’t Generate Taxonomy Archive
- Filter widget outputs
- Adding Page Templates to post but it ignored it
- Order posts by 2 custom fields and if one custom field is zero orderby another custom field