I have multiple custom post types, viz., research, documents, booklets. I have a single archive template for each of them , archive-research.php, archive-documents.php, archive-booklets.php respectively.

I need another template where I display posts from all these CPTs.

For a single CPT, I used the following code:

    'post_type' => 'post_type_name_here',
    'posts_per_page' => 4,

Is there any way to merge theme?

  • What do you mean merge?
    – Howard E
    Apr 9 '19 at 0:46
  • None of your existing templates should be using query_posts(). WordPress will query the correct posts for you. Apr 9 '19 at 1:22
  • Create a new page template and use WP_Query with the post_type key having an array with all your post types.
    – Karun
    Apr 9 '19 at 5:14
  • @Karun that's the answer I needed. I don't know that we can use an array for post_type. Thanks Apr 9 '19 at 6:21
  • @SundayLalbiaknia use this 'post_type' => array('viz', 'research', 'documents', 'booklets'),
    – Karun
    Apr 9 '19 at 6:23

As Jacob Peattle mentioned, you should not be using query_posts in your custom archive templates, that's redundant as WP is already going to query those posts for you. What you really need is pre_get_posts (https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts). That will allow you to conditionally modify the parameters of the main query before it executes. Additionally since you are effectively executing the same query for all 4 CPTs, then it's unnecessary to have 4 separate templates to do so, instead take a look at the template_include filter https://codex.wordpress.org/Plugin_API/Filter_Reference/template_include

Add the following to your functions file...

//Modify the main query    
function custom_archive_query($query){
   if(is_admin() || !$query->is_main_query()){
   $cpts = array("research","documents","booklets");
      $query->set('post_type', $cpts);
add_action('pre_get_posts', 'custom_archive_query');

//Add the template redirect
function custom_archive_template($template){
   $cpts = array("research","documents","booklets");
      $new_template = locate_template( array( 'custom_archive-template.php' ) );
      if(!empty($new_template)) return $new_template;
   return $template;
add_filter('template_include', 'custom_archive_template');

As an additional note, you may need to adjust your query more than this example, and obviously your custom post type names to match. Your original query is paging at 4 posts per page, that may have undesired results due to the fact that you're combining multiple post types.

  • I had the same issue, this solved my problem. Thanks.
    – James
    Mar 31 '20 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.