Skip Navigation

[Resolved] Creating a view that filters by Post Type

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

Problem:
Add a parameter search field on the post type.
Solution:
It is possible within Views filter hook wpv_filter_query, for example:
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"]

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;
}

Relevant Documentation:
https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/

This support ticket is created 8 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
- 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)

Tagged: 

This topic contains 3 replies, has 3 voices.

Last updated by Guillaume 8 years, 2 months ago.

Assisted by: Luo Yang.

Author
Posts
#362143

When creating a view, I was hoping to find "post type" as a post filter option, when building a Query Filter. No such luck.

I have a handful of custom post types and the Page post type.

Is there a way to create a select box that lists out "All Types" by default , but then allows the user to select a specific post type to filter results on?

Thank you,
Aaron

#362255

Dear Aaron,

It is possible within Views filter hook wpv_filter_query, for example:
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;
}

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

And test again.

#362506

Yes! This works great. Thank you so much for the response. I appreciate it.

Aaron

#451979

Hi,
I replace 123 with 142 (wordpress archive view ID), place the code on the theme function.php and put the filter line inside the filter area of the archive view.... but nothing happened on my result page.
i missed something ?
thanks
Guillaume

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