Skip Navigation

[Resolved] Filter view by CPT

This support ticket is created 3 years, 10 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 1 reply, has 2 voices.

Last updated by Luo Yang 3 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1654709

Is there any documentation on how to add a CPT filter to a toolset blocks view. I have a view that includes posts from 4 different post types but I need to have a filter to show any one of those. From what I can see this functionality is not included which is a shame since there is an easy way to add multiple post type to the view, it seams natural to allow this to be a filter. I found some old answers on the forum but the links to the wpv_filter_query docs provided have since moved, So I would like to bring this topic back up and see if there is a recommended method for this or any info out there that is still relevant for Blocks.

#1654935

Hello,

You are right, there isn't such a built-in feature within Toolset Views/Blocks plugin, you can add a feature request for it:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

Our developers will evaluate it.

And the document for filter wpv_filter_query is here:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

Currently, you can try these:
1) edit your view, in section "Filter", add a select dropdown for post type filters into your filter form::

[wpml-string context="wpv-views"]Post Type:[/wpml-string] [wpv-control url_param="wpv-post-type" type="select" values=",page,post" display_values="All,Page,Post"]

You can manually add more post type into it.

2) add below codes into your theme/functions.php:

add_filter( 'wpv_filter_query', 'post_type_filter_func', 10, 3 );
 
function post_type_filter_func( $query_args, $settings, $view_id ) {
    if ( $view_id == 123 && isset($_GET['wpv-post-type'][0]) && !empty($_GET['wpv-post-type'][0]) ) {
        $query_args['post_type'] = $_GET['wpv-post-type'][0];
    }
    return $query_args;
}

add_filter('wpv_filter_register_url_parameters_for_posts', function($attributes, $view_settings){
    if($view_settings['view_id'] == 123){
        $attributes[] = array(
            'query_type'=> 'posts',
            'filter_type'=> 'post_type',
            'value'=> 'custom_field_value',
            'attribute'=> 'wpv-post-type',
            'expected'=> 'string',
        );
    }
    return $attributes;
}, 99, 2);

Please replace 123 with the View's ID of step1)

And test again.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.