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
- Create an archive page by combining two post type
- author archives, showing all custom post types, problem on pagination
- How to clone the “Projects” Custom Post type in Divi theme
- How to make sure content doesn’t display if selection is empty
- How to modify archive query with pre_get_posts to append CPTs?
- Filtering WP_Query Dynamically on the Front-End
- get the custom post type title/name
- Show one item per category of a custom post type
- Why get_posts() not returning only selected category posts from Custom Post Type?
- Storing posts from query and accessing later via AJAX call
- Two near-identical custom field types – one works, the other doesn’t . What can cause this?
- Archive for custom taxonomy lists all posts instead of current taxonomy
- Genesis filterable portfolio isotope [closed]
- Get archive post type name
- Query custom post type with tags
- wp_query check if integer exists in custom field’s array
- Tag Archive for Custom-Post-Type Posts yielding 404 when permalinks set to postname
- convert custom query to wp_query
- Wrong request query on cpt and tax
- Custom post type is showing custom home page, not archive page
- Previous/Next Link by Meta Value in CPT
- Show custom post type filtered by category
- Randomize Posts. Skip the first post in ascending order
- Creating a navigation menu of all posts of a custom post type and their children posts?
- Get custom taxonomies from multiple posts
- URL of a custom post type’s post format archive?
- Pagination for Custom Taxonomy Page [duplicate]
- Search / Filter posts on Title/Content OR Tags
- Pagination on with query_posts in custom post type template
- Query with no terms uses index.php template instead of archive.php
- Wp_query: sort by PHP variable
- WP_Query most viewed posts, in multiple Post Types, last 30 days, excluding a specific taxonomy term
- Custom-Posttype & Custom Taxonomy WP_Query
- Crafting WP_Query array, sort by date
- Custom loop with multiple taxonomy queries
- Return one unique custom post type result when it shares a custom taxonomy in WP_QUERY?
- Custom Post Type with archive page but no detail page
- Converting the_content string to an array?
- forming WP_Query for posts of all post types but from specific categories
- Get latest 3 posts from multiple CPT in one query
- Template for custom post with custom taxonomy
- Loop to fetch 3 post_thumbnail instances from 3 most recent custom post types named “portfolio”
- WordPress Custom Post Type Children Template
- Display upcoming Events for next 7 Days
- Getting template_include to work when allow_url_include is off?
- Custom post type content using custom fields without template
- Custom post type – get_day_link()
- Custom post ui plugin & WP_Query – Polylang
- Change CPT archive title
- 2 Templates 1 custom post type according url
- How can I get the number of custom post type posts that have a specific attachment image set?
- How to use load more custom post type data normal or according to on click category tab In WP
- orderby rand is not working for a custom post types
- Problem querying Custom post type by custom fields
- Orderby CPT custom fields not working
- Custom taxonomy rewrite give pagination 404
- WP_Query order by custom field, then randomly order some of results
- WP_Query inside foreach loop returning same value for all options when filtered using ajax
- Mixing 2 custom post types with posts output in specific pattern
- Custom post taxonomies as tax_query terms?
- How to get post type and month/year before while loop for date.php file