Skip Navigation

[Resolved] Post Type Filter

This support ticket is created 7 years, 2 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 4 replies, has 3 voices.

Last updated by Oliver 7 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#486137

I am trying to: i like to have a view with all entries of all my custom post types. And i like to have a Dropdown Field to filter by post type.

I have the question 6 month ago and you write you will work on a soulution. Is there a date when it comes?

Now i test a soulution in a link:

https://toolset.com/forums/topic/post-type-filter-select/

But nothing is happend when i insert the code.

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(isset($_GET['f_cpt'])){
            $query_args['post_type'] = $_GET['f_cpt'];
        }
    }
    return $query_args;
}

and in view

[wpv-control field="cpt-filter" url_param="f_cpt" type="select" values="0,,all,CPT1,CPT2" display_values="Please select one,All post types,CPT1,CPT2"]

I only see a dropdown field with "Please select one,All post types".

Best

#486216

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

No - post type filter is not added to views yet and its not natively available. I would like to check how you configured your select box of post types as well as what are your post type slug.

You should consider following example - post types post and page:
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 == 9999 && isset($_GET['wpv-post-type'][0]) && !empty($_GET['wpv-post-type'][0]) ) {
        $query_args['post_type'] = $_GET['wpv-post-type'][0];
    }
    return $query_args;
}

Whhere:
- Replace 9999 with your original View ID.

More info:
=> https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/

#486271

Nice, this works for me. Thx a lot!

#588360

This works for me too, but only once. After filtering once by a post type, if I then choose a different post type from the drop down I get the same result from the first filter result.

#588367

I've noticed that the query strings just keep getting longer each time I choose a new post type from the drop down filter. I presume the problem is that the query is not starting afresh each time. How to fix that?

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