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
- Query by post title
- How to check if a WP_Query has data
- Templates for Custom Post Types and Custom Taxonomies
- Number of pages – multiple (custom) post types
- How to quickly switch custom post type singular template?
- Retrieving 3 latest post from each of 5 different custom post types
- Get latest 4 post on a custom post filtered by category
- 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
- Ordering Custom Post Types with WP_Query
- 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
- Archive slider for CPT
- Querying Term Posts in Loop
- Can’t get order_by meta_value_num to work properly
- 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
- Including metaboxes from custom post types in global search — continued
- WordPress Doesn’t Generate Taxonomy Archive
- WordPress custom taxonomy template not working
- WP Query post__in not returning correct results
- Proper way to display latest 5 posts grouped by post type?
- WP Query ‘posts_per_page’
- How can I trace inconsistency in loading a template?
- Can’t access private custom posttype single- or archives-page
- How to Query in WordPress which shows Alphabetic Posts?
- postsperpage value not being applied
- Exclude latest post from WP_Query taxonomy term loop
- Create query for both custom post type and category
- Custom permalink structure for remote content pages
- Getting titles from an array of IDs
- How can I add in post’s the text “No content”
- Specifying a template for custom post type pages
- Custom post type set default template using block editor
- Creating archive like functionality through birectional relationships
- Post template not applying on theme (potentially rendering as Page template)
- Problems in paginate_links with custom query loop and MB-Relationships
- Is it possible to link an external stylesheet, exclusively within a specific single post template?
- How can i remove post type archive URL?
- Custom post type URL structure with site.com/custom_taxonomy_slug/post_name
- How can I find out what template is my custom post type using?
- Custom taxonomy archive slug overwrites static page
- Query events post type after current date and timezone
- Get posts of an specific term of a custom taxonomy
- Retrieve custom post by a query on one of its custom fields
- Conditional posts in WP_query for search
- Get Non-Paginated Index of Post in Paginated Query
- This wp_query will not return any posts and only seems to work with post_status inherit?
- Redirect to different template other than single-custompost.php
- archive.php can’t find categorized posts
- Storing postID in session variable to query database when visitor on custom template page
- How can I set up the URL for a category archive for a custom post type?
- Is it possible to remove the word “date” from archives?
- send user to first page of results when reposting to page?
- Using tax_query to get single post per category
- Show Posts in Vertical Tabs with Scrollbar
- Quering array of post types & pagination. Articles are repeating sometimes on different pages