Skip Navigation

[Resolved] search category filter

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 5 replies, has 3 voices.

Last updated by judeH-2 2 years ago.

Assisted by: Minesh.

Author
Posts
#2529901
toolset-custcode.jpg

Tell us what you are trying to do?
Three weeks ago, I ask about this question in "https://toolset.com/forums/topic/limit-the-number-of-categories-for-search-filter/". I just tried the your suggestion by adding the code in Settings/Custom Code. The following is my code:

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'func_include_exclude_tags', 10, 3 );

function func_include_exclude_tags( $args, $tax, $view_id ){
error_log( "*** func_include_exclude_tags, View ID = " . $view_id . "tax = " . $tax);
if ( $view_id == 12926 && $tax = 'taxonomy-slug' ){
$args['include'] = array( 2, 3, 4, 14, 346 ); // Edit array of term IDs you want to allow of your 4 terms
}
return $args;
}

But I don't see it's working on this page: hidden link even I have activated the code.
The page id 12926, which I used it in view_id. On the left side, I only want these category: Editorials (id = 346), Reviews (id = 2), Analysis (14), Developments (3), and New Voices (4)

In the WP debug.log, I don't see my error_log line, but I see this:

[09-Jan-2023 02:38:39 UTC] PHP Deprecated: Return type of Toolset_Settings::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /data/www/iconnectblog/wordpress/wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/inc/toolset.settings.class.php on line 258

[09-Jan-2023 02:38:39 UTC] PHP Deprecated: Return type of Toolset_Settings::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /data/www/iconnectblog/wordpress/wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/inc/toolset.settings.class.php on line 268

I am using WP 6.1, twentytwentythree theme (Full Site Editing).

Please help

#2530249

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I see you do not replace the 'taxonomy-slug' as suggested with my reply in previous ticket:

Assuming your taxonomy slug is "category" - I've adjusted the following code.
- I've also adjusted the correct view ID. Your view ID is: 12931

add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'func_include_exclude_tags', 10, 3 );

function func_include_exclude_tags( $args, $tax, $view_id ){
error_log( "*** func_include_exclude_tags, View ID = " . $view_id . "tax = " . $tax);
if ( $view_id == 12931  && $tax == 'category' ){
$args['include'] = array( 2, 3, 4, 14, 346 ); // Edit array of term IDs you want to allow of your 4 terms
}
return $args;
}

Can you please try to use the above code that should help you to resolve your issue.

#2530251

Nigel
Supporter

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

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

Hi there

There is an error in the code (it is in the original that you copied from Minesh).

Specifically, when testing if this equals that, you need two (or three) equals signs. When you have only one equals sign you are assigning the value to the variable, not comparing them.

You also didn't change "taxonomy-slug" to match what taxonomy you are using (from your question, I guess it is just in-built categories).

So you should update this line:

if ( $view_id == 12931 && $tax == 'category' ){
#2530673

Thank you for your help. The suggestions worked! What's the right way to find the correct view_id? I thought view_id is the same as page id.

#2530745

Minesh
Supporter

Languages: English (English )

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

Thank you for your help. The suggestions worked! What's the right way to find the correct view_id? I thought view_id is the same as page id.
==>
No, you can see the view ID from view's listing page.

If you are using Toolset blocks then you should activate the legacy mode first to activate the view's menu under Toolset menu:
=> https://toolset.com/course-lesson/enabling-legacy-version-of-toolset-views/

Once you enable the legacy view, refresh the page and you will see the Views menu at: Toolset => Views

Once you visit the view's listing page there you will find the ID column for your view.

#2531251

My issue is resolved now. Thank you!