Support

Account

Home Forums General Issues GET (url) parameters for data arrays? Reply To: GET (url) parameters for data arrays?

  • There are a couple of things here, the first is the LIKE part. Another is that you can have multiple values, so you need to add a separate meta query for and then because of the way you’re doing this you may need to use a nested query with a relationship set, for more info on nested meta queries https://make.wordpress.org/core/2014/10/20/update-on-query-improvements-in-4-1/

    I’m pretty sure this is correct, but I haven’t tested it. This will only work with values that are stored as arrays, like checkboxes. If you have other types of values that can be submitted in the url then you’d need to do a switch on $name and construct each nested query to match the way the type of is stored.

    
      
      function my_pre_get_posts( $query ) {
      
      // bail early if is in admin
      if (is_admin()) {
        return;
      }
      // get meta query
      $meta_query = $query->get('meta_query');
      
      // loop over filters
      foreach ($GLOBALS['my_query_filters'] as $key => $name) {
        $nested_query = array();
        // continue if not found in url
        if( empty($_GET[ $name ]) ) {
          continue;
        }
        // get the value for this filter
        // eg: http://www.website.com/events?city=melbourne,sydney
        $values = explode(',', $_GET[ $name ]);
        $nested_query['relation'] = 'OR';
        foreach ($values as $value) {
          $sub_query = array(
            'key' => $name,
            'value' => '"'.$value.'"',
            'compare' => 'LIKE',
          );
        }
        $meta_query[] = $nested_query;
      } 
      // update meta query
      $query->set('meta_query', $meta_query);
    }