Skip Navigation

[Resolved] One archive template for multiple custom post type

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to create 1 single view but use it across multiple different archive and switching the post type of the view.

Solution:

This is possible by overwriting the post type query for your view.

Have a look at the custom query below.


add_filter( 'wpv_filter_query', 'filter_post_type_func', 99, 3 );
    
function filter_post_type_func( $query_args, $view_settings, $view_id ) {
    if ( in_array($view_id, array(20356)) ) {
         if ( is_post_type_archive( 'CPTsluggoes here' ) ){
            $query_args['post_type'] = 'cptslug goes here';
        }
  if ( is_post_type_archive( 'CPTsluggoes here' ) ){
            $query_args['post_type'] = 'cptslug goes here';
        }
  if ( is_post_type_archive( 'CPTsluggoes here ) ){
            $query_args['post_type'] = 'cptslug goes here';
        }
    }
    return $query_args;
}

Replace the 20356 with the ID of your view and then replace "CPTsluggoes here" with the relevant slugs of your Custom Post Types.

This support ticket is created 5 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by felixG-2 5 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#1355193

I have 3 custom post types, namely State, City & Development.

I wish to create only 1 template for the 3 CPTs above using Elementor and insert it using a View, 3 of the CPTs sharing same structure, I able to get Elementor to share the template among the 3 CPTs. I created the view with Content Selection of 3 CPTs, the loop combine all the posts from the CPTs as a final result at the front-end regardless of the current CPT.

Is there anyway in View to identify the current CPT and display only the relevant posts without having to create multiple templates/views?

#1355325

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Felix,

Thank you for contacting our support forum.

We might be able to do this by modifying the view query by determining the post type of the archive.


add_filter( 'wpv_filter_query', 'filter_post_type_func', 99, 3 );
   
function filter_post_type_func( $query_args, $view_settings, $view_id ) {
    if ( in_array($view_id, array(20356)) ) {
         if ( is_post_type_archive( 'CPTsluggoes here' ) ){
            $query_args['post_type'] = 'cptslug goes here';
        }
  if ( is_post_type_archive( 'CPTsluggoes here' ) ){
            $query_args['post_type'] = 'cptslug goes here';
        }
  if ( is_post_type_archive( 'CPTsluggoes here ) ){
            $query_args['post_type'] = 'cptslug goes here';
        }
    }
    return $query_args;
}

Please try the code above, replace 20356 with the ID of your view as well as you will need to add the slug of your CPT to the conditionals so that it can check if the current archive matches that post type.

Add the code to your toolset custom code section in Toolset->Settings->Custom Code and activate it.

Please let me know if this helps.

Thanks,
Shane
However

#1355421

Works perfectly.

My issue is resolved now. Thank you!