Skip Navigation

[Resolved] 'Greater than' and 'Less than' comparisons (in the same field)

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: Asia/Karachi (GMT+05:00)

This topic contains 12 replies, has 2 voices.

Last updated by anneM-2 3 years, 9 months ago.

Assisted by: Waqar.

Author
Posts
#1687589

Is it possible to add both a 'greater than' and a 'less than' comparison in a single search field

To explain further, I am setting up a search function for a customer to select a product based on a capacity i.e. 200kW. If the user inputs '200' into the 'kW Capacity' field then I would like the function to return all products that are between 5% less than '200' and 15% greater than 200.

Is this possible?

Thanks,

#1688481

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

I'm afraid, there is no built-in feature available to achieve this, so it will require some code customization.

Can you please share temporary admin login details, along with a link to a page where this view/search can be seen?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Waqar

#1690771

Hi Waqar,

Did you get a chance to look at the issue on the site yet? I would really appreciate some help with this. I have now moved away from the WP Import Pro sandbox and have the Search on the development site that I am working on.

Access is currently restricted by IP, but if you can let me know when you will look into it, I will temporarily lift the restriction....or let me know your IP and I will add it to the permitted list.

Thanks in advance,

Anne

#1692315

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Anne,

Thank you for waiting and I apologize for the delay in getting back on this.

I've seen how the search is set on the homepage and understand the requirement. I'm currently doing some testing on my website, to suggest the next steps and will share my findings as soon as it completes.

Thank you for your patience.

regards,
Waqar

#1692365

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Anne,

On my test website, I was able to make this work, using the "wpv_filter_query" hook to customize the query.
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )

1. In the view's search, I included the search field for the "nominal-capacity" field, with simple "equal to" numeric comparison.
( screenshot: hidden link )

2. In the active theme's "functions.php" file, I included the following code to filter the query to split the actual searched value into minimum and maximum ranges:


add_filter( 'wpv_filter_query', 'filter_products_view_test', 1000 , 3 );
function filter_products_view_test( $query_args, $view_settings ) {
 
    if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 12345) ) {

        // slug of the target field
        $target_field_slug = "wpcf-nominal-capacity";

        // check for the existing meta query filters
        if(!empty($query_args['meta_query'])) {
            foreach ($query_args['meta_query'] as $meta_query_arr ) {
                if( (!empty($meta_query_arr['key'])) && (isset($meta_query_arr['key'])) ) {
                    $available_keys[] = $meta_query_arr['key'];
                }
            }
        }
        else {
            $available_keys[] = '';
        }

        // if filter for the target field exists customize it
        if (in_array($target_field_slug, $available_keys)) {
            $target_key = array_search($target_field_slug, $available_keys);
            $target_value = $query_args['meta_query'][$target_key]['value'];

            // 5% less of the search value
            $target_value_min = round( ($target_value) - ( (5*$target_value) / 100 ) );
            // 15% less of the search value
            $target_value_max = round( ($target_value) + ( (15*$target_value) / 100 ) );

            $query_args['meta_query'][$target_key] = array(
                'key' => $target_field_slug,
                'value' => $target_value_min.','.$target_value_max,
                'type' => 'NUMERIC',
                'compare' => 'BETWEEN'
            ); 
        }
     
    }
 
    return $query_args;
}

Please replace "12345" with the actual ID of your view and "nominal-capacity" with the actual slug of your target custom field.

Important: This kind of custom query filtering will not work, if the view is set to update the results through AJAX, that is without reloading the page.
( screenshot: hidden link )

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1697677

Hi Waqar,

I have followed the steps and instructions above but the code isn't working, it only returns exact matches.

I've set up on the other site and would appreciate it if you could take a look, either on the site back-end or in the code below that is copied from the theme functions.php file. Just let me know please, and I will send you the log-in details.

---------------------------------------------------------------------------------------------------------------------

add_filter( 'wpv_filter_query', 'filter_products_view_test', 1000 , 3 );
function filter_products_view_test( $query_args, $view_settings ) {

if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 3989) ) {

// slug of the target field
$target_field_slug = "wpcf-capacity-kw";

// check for the existing meta query filters
if(!empty($query_args['meta_query'])) {
foreach ($query_args['meta_query'] as $meta_query_arr ) {
if( (!empty($meta_query_arr['key'])) && (isset($meta_query_arr['key'])) ) {
$available_keys[] = $meta_query_arr['key'];
}
}
}
else {
$available_keys[] = '';
}

// if filter for the target field exists customize it
if (in_array($target_field_slug, $available_keys)) {
$target_key = array_search($target_field_slug, $available_keys);
$target_value = $query_args['meta_query'][$target_key]['value'];

// 5% less of the search value
$target_value_min = round( ($target_value) - ( (5*$target_value) / 100 ) );
// 15% more of the search value
$target_value_max = round( ($target_value) + ( (15*$target_value) / 100 ) );

$query_args['meta_query'][$target_key] = array(
'key' => $target_field_slug,
'value' => $target_value_min.','.$target_value_max,
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
);
}

}

return $query_args;
}

What is also happening is that all of the fields in the search results are now pre-fixed with '?'. Don't know if that helps?

Thanks again,

Anne

#1704893

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Anne,

Thanks for writing back and sorry to learn that the code is not working for you.

Can you please share a clone/snapshot of the website where you tested this?
( ref: https://toolset.com/faq/provide-supporters-copy-site/ )

This will be more efficient as I'll be able to troubleshoot this in more depth on my own server.

Note: I've set your next reply as private.

regards,
Waqar

#1706213

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing these details.

I tried to download the duplicator package, but Google Drive is asking for access permission. Can you please grant this permission so that I can download and perform some tests?

With the duplicator package, the admin area or FTP access for your actual website won't be needed.

#1706325

Dear Waqar,

Please try again now.

#1706397

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for the permission and I have downloaded the package.

I'll be doing some troubleshooting on this clone and will share my findings, as soon as it completes.

Thank you for your patience.

#1706519
EER error.jpg

Thank you.

If possible could you also please investigate why I am getting this (screenshot of frontend below) whenever I put a value in the 'EER' search field. I've tried various field settings (number/decimal/string) and Type of Control to include 'as defined' and 'Input as Text', but the problem persists.

Thanks,

#1708719

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for waiting.

During troubleshooting on your website's clone, I noticed that the code snippet was set to use the view ID "3989".

This is actually the ID of the content template "Product Selector" that holds the view, but not the ID of the view itself.

The ID of the "Product Selector" view is "3990" and I've updated this in the code and it started working as expected.

Tip: To access the views management screen at WP Admin -> Toolset -> Views, you can temporarily set "Editing experience" option at WP Admin -> Toolset -> Settings -> General to "Show both the legacy and Blocks interface and let me choose which to use for each item I build".

As for the issue with the 'EER' search field, it is the result of the third-party plugin "Tooltips" which adds a Tooltips HTML around the text "eer" in the search query generated by the View and as a result, breaks it.

You can consult that plugin's official support or documentation, to learn around how to include or exclude specific section text for Tooltips processing.

I hope this helps and please let me know if you need any further assistance around this.

#1708725

My issue is resolved now. Thank you!

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