Skip Navigation

[Resolved] Filter view by custom range of values

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user was using custom code to hook into a view query filter. The custom code was crashing in some cases.

Solution:
The code was not optimized for all cases and needed a check to succeed. Check the check in line 12 on this reply https://toolset.com/forums/topic/filter-view-by-custom-range-of-values/#post-1790475

This support ticket is created 4 years, 1 month 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 4 replies, has 2 voices.

Last updated by Dan Kitsmiller 4 years, 1 month ago.

Assisted by: Jamal.

Author
Posts
#1786813

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?

#1787915

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?

#1789583

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

#1790475

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.

#1790855

Thank you, that did the trick! My issue is resolved now. Thank you!