Skip Navigation

[Resolved] Frontend search filter by post types

This support ticket is created 3 years, 9 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: Africa/Casablanca (GMT+01:00)

This topic contains 7 replies, has 3 voices.

Last updated by Jamal 3 years, 9 months ago.

Assisted by: Jamal.

Author
Posts
#2153553

Hi,
I generating search results as WordPress Archive for search with different post types. Now I want to add a post type filter in the front end. How to achieve that? Please help.
See the search result page here: hidden link
Thanks.

#2153705

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Custom search with Views and WordPress Archives doesn't support filtering by standard post fields such as post_type.

However, the legacy editor for Views and Archives did allow you to insert a filter on the front end that is nominally connected to some query field which isn't actually used, so that you can then add some custom code to modify the query based upon what is selected in the filter.

So in the filter section of your custom archive, you can add a wpv-control shortcode like so:

[wpv-control field="dummy" url_param="filterby" type="select" values=",post,page,custom-cpt" display_values="--select--,Post,Page,Custom CPT"]

You will likely want to wrap this with some markup for a form label etc.

Then you would need to add some code to modify the query if any filter value is provided. Because this is an archive (rather than a View) you will need to use the core pre_get_posts hook, like so:

add_action( 'pre_get_posts', 'ts_filter_archive', PHP_INT_MAX - 1, 1 );
function ts_filter_archive( $query ){

    if ( $query->is_main_query() && $query->is_search() && isset( $_GET['filterby'] ) && is_array( $_GET['filterby'] ) ){

        $query->set( 'post_type', $_GET['filterby'] );

    }
}

You could add that at Toolset > Settings > Custom Code.

#2154179

Well it works fine but have some issues.
1. The reset button is not working. You can check with the previous instance I have given above.
2. The check box once I click stays selected even if I uncheck it and select another one for fresh search filtering.
3. The url keeps on growing with every submit button I click.
4. The archive is coming up with a pagination without my control.

#2154205

Even if I have mentioned some problems earlier (4 points), I found that "AJAX results update when visitors change any filter values" works fine. So I would rather stick to this style.

But the problem is the filter is removed as soon as I go to other pages of the search results. Thanks for your help.

#2154255

Hello and thank you for contacting Toolset Support.

If you want to have the filters selected after returning from a result page, you will need to reply on URL parameters. Even if you use the AJAX search, you can activate browser history in Toolset Settings. However, there may still be some issues with the post type filter. I'd like to take a closer look at your website and check this further. Your next reply will be private to let you share credentials safely. ** Please take a database backup before sharing credentials. **

#2155459

Unfortunately, I am unable to log in. I get redirected to the profile page and I am asked to perform a 2FA authentication, but I can't enter the code from Google Authenticator, the page just does not let me enter the 4th 5th, and 6th numbers of the code.

Can you disable 2FA for this user?

#2155533

I have disabled the 2factor authentication for you. Please check. Thanks.

#2156129

Thank you, I can log in now and see the archive template.

Regarding the pagination, the second one is added by the theme, and I can't really tell, right now, how to remove it. But you can hide it with the following CSS code in the archive template:

div#primary>div.ast-pagination {
    display: none;
}

Regarding the URL issues, I am afraid, any solution would be very hacky and may work in cases and break cases. The best solution I can think of is to mirror the post type into a custom field or a custom taxonomy. This way you will get working filters that handle the URL parameters correctly.

Such mirroring can be done by a snippet of code that will sync the post type, into the custom field or the custom taxonomy, after each update of a post. Basically after the save_post action https://developer.wordpress.org/reference/hooks/save_post/
Let me know if you need help with such a snippet.

If you believe that filter by post type should be added to Toolset, please ask it here https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/
Our product managers prioritize features requests based on several factors, including how many users ask for it.