Skip Navigation

[Resolved] Search results to show value 1 and 3 when 1 is selected in Toolset

This support ticket is created 3 years, 10 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by pramodk-2 3 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1895191
Search filter three V1.0.png
Search filter two V1.0.png
Search filter one V1.0.png

Hi Team,

In my requirement, there is a search query name 'Gender'.

(i) When user selects 'Male' then the results should show for both 'Male' and 'Any (Male or Female)' .
(ii) When user selects 'Female' then the results should show for both 'Female' and 'Any (Male or Female)'.
(iii) When user selects 'Any (Male or Female)' then the results should show only for 'Any (Male or Female)'.

Referred to the code in the below link and modified accordingly.
https://toolset.com/forums/topic/filtering-using-manually-entered-values/

add_filter( 'wpv_filter_query', 'custom_search_criteria',10,2 );
function custom_search_criteria( $query_args ,$view_settings ) {
if (isset($view_settings['view_id']) && $view_settings['view_id'] == 641 ) {
foreach((array)$query_args['meta_query'] as $k=>$v):
            if(isset($v['key']) and $v['key']=='wpcf-flatmate-preference-gender'){
                if($v['value']==1){
                    $query_args['meta_query'][$k]['compare']= 'IN';
                    $query_args['meta_query'][$k]['value']= array(1,3);
                }else if($v['value']==2){
                    $query_args['meta_query'][$k]['compare']= 'IN';
                    $query_args['meta_query'][$k]['value']= array(2,3);
                }
            }
        endforeach;
}
return $query_args;
}

Field slug = 'wpcf-flatmate-preference-gender'.
Archive ID= 641 ( Is this the value I would need to replace for view ID ?)

Please find attached the images for more details.

However, there is no change in the search results page.

Please suggest.

hidden link

Thanks.

#1895397

Hi, the main problem I see here is the wpv_filter_query filter is not intended for use with WordPress Archives, and it will have no effect on the results here. The wpv_filter_query filter only applies to Views. If you want to apply this type of filter, you would have to recreate this WP Archive as a View, then the wpv_filter_query filter can be used to programmatically manipulate the query criteria like this. Typically you would place this View on a custom Page, instead of placing it inside a WP Archive.

#1895403

Hi Christian,

Thanks for your response.

As per my requirements and considering SEO, the search query should be in an archive page.

Please suggest me on how do I achieve if it is in an archive page.

Thanks.

#1895759

You could build the View as I described using the legacy Views editor, then insert the View in a WP Archive using a shortcode. If you are using the Block Editor to design the WP Archive, you must place the View somewhere outside the archive loop block.

#1896039

Thanks Christian,

Will try with the suggestion provided.

There are 3 values below in the 'Gender' field.

Male - 1
Female - 2
Any (Male or Female) - 3

There is no default value for this field and if I do not tick required (which is not mandatory) field, then there are chances that customer can leave it as blank (empty).

In the search query, if the customer selects Male (Value '1') then I would expect the result for Male (Value '1') and null (empty).

Please suggest on how we achieve this?

Thanks.

#1896467

It sounds like you want to show results where the custom field value is equal to 1, or where the custom field value NOT EXISTS.

(custom field value = 1 ) OR (custom field NOT EXISTS)

That type of filter comparison is not possible in Views/Blocks, but you may be able to achieve it with custom code. I did a quick search and found some related information, including examples showing how to filter by a custom field NOT EXISTS combined with another filter for the same custom field equal to a specific value:
https://wordpress.stackexchange.com/questions/80303/query-all-posts-where-a-meta-key-does-not-exist
https://developer.wordpress.org/reference/classes/wp_meta_query/

Hopefully this helps? Let me know if I have misunderstood what you want to achieve.

#1896545

My issue is resolved now. Thank you!