You can hook into the pre_get_posts
action, to access the $query
object.
You should use your action hook inside your functions.php.
An example of what your function could look like:
add_action( 'pre_get_posts', 'publications_archive_query' );
function publications_archive_query( $query ) {
if ( !is_admin() && $query->is_main_query()) {
if ( $query->get('post_type') === 'publications' ) {
$query->set( 'posts_per_page', 5 );
}
}
Narrow down what is the query you are modifying by using conditional checks.
$query->get('post_type')
to get current’s Query post_type to check against.
is_main_query
to make sure you are only applying your query modification to the main query.
Read further and find more examples:
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
Related Posts:
- How to remove custom post type archive
- How to use a custom post type archive as front page?
- Page Template as Custom Post Type Archive
- Custom post type yearly/ monthly archive permalinks
- Can’t get post id on page that is a custom post type archive
- Meta_Query as a way how to setup CPT permalinks – is it a good thing?
- Custom Post Type Archives with 0 Posts Redirects as 404
- Get monthly archives for custom post type
- Is it possible to have an index page for taxonomy term for each custom post type it is assigned to?
- How to get_queried_object on multiple objects?
- Assign Page Template Within A Custom Post Type
- Custom Post Type / Taxonomy Slug / Post Title with post type archive
- Posts per Page on custom Taxonomy Template
- Post type archive page not working
- Custom Post Type – Archive page title
- Should I change these content meta types before building complex archive page?
- Getting taxonomy terms used by custom post type
- Archive or taxonomy pages not working for custom post type
- How to create archive page for taxonomy terms within custom post type
- How to list custom post types?
- I am facing problem in my archive page , it is partially working
- taxonomy template for custom post type the same as the archive
- Display only post types that are direct children of the current taxonomy
- Loop that displays the first post of every available custom post type?
- Custom post type search with slug in URL
- How can I give CPT archive correct body class?
- Conditional tags don’t work
- Specify a Page as the parent to the CPT Archive
- Custom template won’t load for a custom post type (custom permalinks used)
- Changing number of posts per page on CPT-archive.php, have tried 20-30 code chunks so far
- Unable to get post archive link
- CPT custom archive template does not get loaded
- Custom post type is showing custom home page, not archive page
- URL of a custom post type’s post format archive?
- Custom Post Type archive loop separated by term, in specific term order
- Show only posts from one category on custom post type archive page
- Create template for taxonomy results limited by Custom Post Type
- How should I structure my post types?
- Custom post type: Disable single page, but keep archive
- Use Custom Post Type archive page for the taxonomies term archive page
- Display custom post type category, while in a CPT category, then the posts beneath
- Number of Custom Post Types published are not being shown in the custom page
- Custom Post Type has_archive
- Custom Post Type Archive URL is wrong
- list archives per post types
- Canonical url differs from custom post type archive URLs
- Display a custom posts page for post type
- Custom post type archive pagination 404
- posttype/taxonomy/term archive page 404 error
- Ignoring slug capitalization on rewrite rule for custom post type archive page
- My archive-posttype.php template is not loading
- Custom Post Archive is not working
- Custom post type archive with page as parent url
- Set featured image to archive.php
- Custom post type, global categories — what’s the template name?
- Url to archive page for custom post type
- How can I use archive-{post_type}.php theme template?
- How to display CPT archives?
- Custom meta Title for custom post type archive from page
- Return the name of the post type
- current-menu-item not added on page showing custom post type (rendered with archive template)
- Alphabetically sort a taxonomy.php template by post title
- Custom Post type date archive for custom taxonomy
- adding custom post type “name” to single template
- Custom Post Type Archive template not being called
- Custom Post Type Custom Archive Page Not Working
- Getting custom taxonomy posts on archive page
- Custom post type category permalinks and archive pages
- Displaying custom taxonomy menu in custom post type archive
- Pages not appearing?
- Is possible register two archive pages for single custom post type?
- CPT archive with WP native/core tax/categories
- How to add specific terms in a custom post type?
- WordPress Page hierarchy ( parent is singular of custom post types slug ) is returning not found
- Static Front Page not working for custom theme
- Display Custom Post Type Archive content with Shortcode
- Custom Post Type archive page listing a 404 or single post
- Building Link List for Custom Tax
- Unsure what page hierarchy to use with CPT UI to show on front end
- Only show categories that have posts within custom post type
- get first limited words from a custom post in the archive page
- Meta_query by date for Events archive
- How to manage a custom post type archive as a page?
- Custom post types not displaying per category
- How to Show all Values in category.php page using post_type
- Show index and not the archive for “Dog Custom Post” with default slug
- Custom post type archive page not showing on archive-posttype.php
- Making a archive gallery page for a custom post type which already has an archive page
- Custom taxonomy in custom post type archive with pagination
- why custom post archive gives always no more than 10 posts despite of paging settings?
- Templates list in “Page Attributes” metabox is inaccurate
- Stop Custom post type from being searched via URL
- title tag for custom post type remove taxonomy name from title tag
- How can I retrieve the slug for a custom post type via name?
- paging is not working properly on news archives page [duplicate]
- custom post type not getting archived?
- Pagination not working for archive
- Remove plugin’s custom post type archive page and single pages [closed]
- Add a class to a menu item depending on a body class
- Custom archive URL as a subfolder of the custom post type slug