Never use new WP_Query
for the main query of an archive template. WordPress has already queried the correct posts and things like pagination are based on that query. If you want to modify that query you should use the pre_get_posts
to modify the main query before it is run.
The hook runs on every query that is performed, and the WP_Query
object is passed a parameter so that you can modify it. You can use its various methods to identify which query you’re modifying.
To modify the main archive of a resource
post type to also display posts your code would look like this:
add_action(
'pre_get_posts',
function( $query ) {
if ( ! is_admin() && $query->is_post_type_archive( 'resource' ) ) {
$query->set( 'post_type', [ 'resource', 'post' ] );
}
}
);
Related Posts:
- Pagination not working with custom loop
- How to use a custom post type as front page?
- pagination not working for category.php (custom post types in categories)
- Custom post type pagination error
- Pagination Not Working When Used With WP_Query() `offset` Property
- Is it possible to add query parameters on the archive page?
- Pagination not working with custom loop
- Pagination not working with custom loop
- Pagination not working with custom loop
- Pagination not working with custom loop
- Pagination not working with custom loop
- Fourth page of custom post type archive page does not exist
- How do you get the count of posts in an archive page?
- wp_pagenavi() with custom wp_query()?
- Custom Post Type Pagination Not Working On Archive Page
- Archive page…limiting posts per page
- Related posts by author pagination not working on the production site
- Secondary loop pagination on custom post type single post gets redirected to first page
- Pagination go to first page if i’m on last post
- Querying Posts by Taxonomy From Alternate Network Site
- Using new WP_Query in shortcode in a custom field causes the main post content to not display
- custom post type paging not working past page 3
- Should unaltered default loop/query return a list of custom posts belonging to a custom taxonomy?
- Transient pagination not working properly
- WP_Query pagination using only numbers instead of /page/1 on URL
- exclude custom post type by meta key in wp_query
- Query for posts in 2 taxonomies
- WP_Query search posts by custom post type and custom taxonomy
- Unable to get post content from custom post type loop
- Calculating Bayesian average for custom post type
- Create ONE callback for all page templates, post filter queries + paginated pages, triggering pagination via AJAX
- Remove duplicated values from a loop
- WordPress Orderby Numeric Value Not Working
- Custom Post Type Loop within Shortcode
- How to connect two custom post types with nested loops
- How to add pagination to wp_query [duplicate]
- Display CPT posts based on specific taxonomy
- Show one item per category of a custom post type
- convert custom query to wp_query
- Pagination on with query_posts in custom post type template
- Pagination issue on category.php using custom post type query
- Problem: wp_query outputs all images on site
- First custom field value (out of several) displayed twice after query
- Prioritize posts in query by meta keys?
- WP_Query orderby and tax_query
- Custom Theme With Custom Loops
- Custom Post Types strange pagination problem
- Different Ways to Query Custom Post Types?
- can these 3 queries be re-written as 1 query?
- Wrapping x posts in html without leaving empty html
- Number of posts in the archive
- Loop increase number
- Using wp_query to modify the loop in index.php for a CPT
- Custom WP_Query doesn’t display all posts
- WP_Query() with custom post type and taxonomy — get all terms?
- Query & the_content() is showing in the header
- Listing custom post types on archive page with array
- How to conditionally add Custom Post Type to Front Page
- Pagination not working on custom query on a page
- Custom post type and custom taxonomy 404 on page 2
- Pagination on search results page
- All blog posts are not showing up
- Query custom posts from custom taxonomy
- How do I list a custom field and custom taxonomies for each result in a loop?
- Custom Post Type Query W/Category Dropdown
- Changing WordPress sort order for returned child pages
- Multiple custom post type queries causing wrong post types to be grabbed in taxonomy + single templates?
- Query for specific taxonomy that executes a particular loop depending on volume of posts?
- Page that lists publications by classifying them by taxonomy
- Change search query in wordpress custom post type
- Exclude a specific post in a Custom Post Type
- Pin posts to top of custom loop
- Pagination not working in custom post type. Help
- Filtering posts based on three taxonomies
- How select query is generated in a Custom Post Type?
- Error in the page of a CPT file in WordPress
- WordPress doesn’t respect Page slug in custom post type url pagination. Keeps getting removed
- post type => ‘any’ not applied my custom queries
- Linking posts together with Advanced Custom Fields “both ways”
- 2 custom posts types, in their own columns, one pagination for both
- WordPress query in which condition uses custom field
- WordPress loop add heading before first of type
- Error in WP Query. If variable is empty it is displaying previous post value
- why custom post archive gives always no more than 10 posts despite of paging settings?
- Pagination on custom post types
- Pagination in custom post type page template
- query and paginate multiple post types
- Post data in separate divs with incrementing class using WP_Query
- Query child posts on parents single.php?
- Paged within a custom content type template [duplicate]
- WordPress can only query up to 766
- next_posts_link returns same content of 1st page
- Custom loop won’t work, can’t find problem
- Create a list of months based posts
- How do I correctly query posts from a post ID?
- display custom post type from register taxonomy
- How to exclude certain portfolios from a loop
- Custom post type and have_posts() return empty result
- How to insert a post from a different post type after every nth post
- Complex Custom Loop with Includes