I'm filtering a view by a range of values, using this code I found elsewhere in the forum:
/**
* Sets values of Price Range dropdown menu
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
add_filter('wpv_filter_query', 'search_between_numeric_func', 10, 3);
function search_between_numeric_func($query_args, $setting, $view_id) {
if($view_id == 53 OR $view_id == 3144) {
foreach((array)$query_args['meta_query'] as $k=>$v):
unset($query_args['meta_query'][$k]);
if(isset($v['key']) and $v['key']=='wpcf-price'){
$query_args['meta_query'][$k]['key'] = 'wpcf-price';
$query_args['meta_query'][$k]['type'] = 'NUMERIC';
$query_args['meta_query'][$k]['compare'] = 'BETWEEN';
$query_args['meta_query'][$k]['value'] = str_replace("-",",",$v['value']);
}
endforeach;
$query_args['meta_query']['relation'] = 'AND';
}
return $query_args;
}
It works as expected, but if a user visits the page directly (without a url paramater), it generates this error:
Notice: Undefined index: meta_query in /var/www/wp-content/toolset-customizations/range-slider.php on line 13
How can I fix this error and either show all results or no results?
Hello and thank you for contacting the Toolset support.
I do not think this code is correct. I can see an error on line 13( the (array) should not appear). And I think that line 15 should come after line 16. Can you share the URL of the page where you found this code?
On the other hand, I wonder, why would you filter the view with custom code instead of using the user interface? Can you elaborate on this a little?
Hi Jamal,
I revised the code per your instructions. It returns the correct results as expected, but still gives errors if no URL parameter is supplied. Here is the revised code:
/**
* Sets values of Price Range dropdown menu
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
add_filter('wpv_filter_query', 'search_between_numeric_func', 10, 3);
function search_between_numeric_func($query_args, $setting, $view_id) {
if($view_id == 53 OR $view_id == 3144) {
foreach($query_args['meta_query'] as $k=>$v):
if(isset($v['key']) and $v['key']=='wpcf-price'){
unset($query_args['meta_query'][$k]);
$query_args['meta_query'][$k]['key'] = 'wpcf-price';
$query_args['meta_query'][$k]['type'] = 'NUMERIC';
$query_args['meta_query'][$k]['compare'] = 'BETWEEN';
$query_args['meta_query'][$k]['value'] = str_replace("-",",",$v['value']);
}
endforeach;
$query_args['meta_query']['relation'] = 'AND';
}
return $query_args;
}
It's filtered with custom code because I'm using a single dropdown menu to set both a minimum and maximum value for the search. Here is the thread that I used as a starting point: https://toolset.com/forums/topic/custom-search-filter-price-range-dropdown/
Here are the errors that are thrown if no URL parameter is supplied:
Notice: Undefined index: meta_query in /var/www/wp-content/toolset-customizations/range-slider.php on line 13
Warning: Invalid argument supplied for foreach() in /var/www/wp-content/toolset-customizations/range-slider.php on line 13
Thank you for your feedback, and my apologies for my last suggestions, I checked with the team and they are not correct.
Please check the following code:
/**
* Sets values of Price Range dropdown menu
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
add_filter('wpv_filter_query', 'search_between_numeric_func', 10, 3);
function search_between_numeric_func($query_args, $setting, $view_id) {
if($view_id == 53 OR $view_id == 3144) {
if ( isset( $query_args['meta_query'] ) ) {
foreach((array)$query_args['meta_query'] as $k=>$v):
unset($query_args['meta_query'][$k]);
if(isset($v['key']) and $v['key']=='wpcf-price'){
$query_args['meta_query'][$k]['key'] = 'wpcf-price';
$query_args['meta_query'][$k]['type'] = 'NUMERIC';
$query_args['meta_query'][$k]['compare'] = 'BETWEEN';
$query_args['meta_query'][$k]['value'] = str_replace("-",",",$v['value']);
}
endforeach;
$query_args['meta_query']['relation'] = 'AND';
}
}
return $query_args;
}
If this does not work, please allow me temporary access to your website to check this closely. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
Please provide more details about what view, on what page is it used.
Thank you, that did the trick! My issue is resolved now. Thank you!