Skip Navigation

[Resolved] Create a search view that allows the user to select multiple different post type

This support ticket is created 4 years, 5 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 3 replies, has 2 voices.

Last updated by Minesh 4 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#1661849

Tell us what you are trying to do?

OK, so I'm new to Toolset and just investigating what I can do with it. What I'm trying to do feels like it should be possible and pretty simple to achieve. I'd like to add a search field with multiple checkboxes to allow the user to restrict their search to different post types.

e.g:

[ / ] Farmers Markets
[ / ] Restaurants
[ ] Suppliers

But I can't find an appropriate Search Field to add to my View. note I'm using Toolset Blocks.

Many thanks in advance.

#1662773

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Great to know that you chose Toolset to build your site.

There is no native feature available with Toolset Blocks using which you can filter your post based on post type. However, this is known feature to us and we already have in on our Development plan and this feature will be added in near future.

However, there is workaround and for that you need to create the view using classic view instead of blocks. To enable the classic view on your site, you need to follow the steps given with the following link:
=> https://toolset.com/2019/12/using-toolset-blocks-on-existing-toolset-sites/
[Please follow the section "Enable the new Blocks workflow"]

Once you set the above settings, you will see the "Views" as submenu under Toolset menu. Toolset => Views.

Now, you should create a custom search view from Toolset => Views => Add New.

1) edit your view, in section "Search and pagination", 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:
or
You can add it to custom code section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

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

Where:
- Replace 9999 with your original View ID.

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

Please check the following related tickets that may help you:
=> https://toolset.com/forums/topic/creating-a-view-that-filters-by-post-type/

Please let me know if you need further hep.

#1664359

Thank you for your reply. This is quite complicated, but I think I'm making progress.
I've created the view and inserted it into a page. I've added the PHP code snippet using Tooleset -> Settings -> Custom Code

I have some issues though. I've changed the "post typ" form element type to "checkboxes". The form displays OK, and allows me to check and uncheck the different post types. But when I submit, the form re-checks all the boxes and it won't return any results.

I think I might abandon this and just use the blocks editor and make different forms for different post types.

Not really what I wanted, but this is not really working for me.

#1664365

Minesh
Supporter

Languages: English (English )

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

If you need further help with the workaround I would be happy to help further bug if you think that this is not how you want to implement it, as I shared information in my previous reply that there is no filter available to filter the posts by post types.

In that case, if you want to use blocks, you can create different views per post types if that makes your life easy. If you need further help with workaround, Please feel free to get in touch with me. Please let me know your decision.