Support

Account

Home Forums General Issues Checkbox filter – multiple queries

Helping

Checkbox filter – multiple queries

  • Hi,

    I am trying to achieve a checkbox filter (yep, that old chestnut!) that filters multiple queries.

    So far, i have used the code that is supplied on the tutorial page
    https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/ as well as some modified code from the forums, im nearly there, just one final hurdle.

    I have two queries set up using ACF checkboxes:

    Rooms: 4, 5, 6
    City: Sydney, Melbourne

    And the following posts –

    5 rooms in Melbourne
    6 rooms Melbourne
    4 rooms Sydney

    This query returns the correct post: http://example.com/homes/?rooms=5&city=melbourne

    this query however returns nothing: http://example.com/homes/?rooms=5,6&city=melbourne

    I would expect the following query to return two posts, but again it returns nothing: http://example.com/homes/?rooms=5,6

    Here is the code i’m using, if anyone could point me in the right direction it would be appreciated.

    
    // array of filters (field key => field name)
    $GLOBALS['my_query_filters'] = array(
      'field_589726fd3ce6c' => 'rooms',
      'field_58a2043d3d362' => 'city'
    );
    
    // action
    add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);
    
    function my_pre_get_posts( $query ) {
     
      // bail early if is in admin
      if( is_admin() ) return;
     
     
      // bail early if not main query
      // - allows custom code / plugins to continue working
      if( !$query->is_main_query() ) return;
     
     
      // get meta query
      $meta_query = $query->get('meta_query');
    
           
      // loop over filters
      foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
        
        // continue if not found in url
        if( empty($_GET[ $name ]) ) {
          
          continue;
          
        }
        
     
       $values = explode(',', $_GET[ $name ]);
     
    
      foreach( $values as $value ){
        // append meta query
    
          $meta_query['relation'] = 'AND';
    
          $meta_query[] = array(
                'key'   => $name,
                'value'   => '"' . $value . '"',
                'compare' => 'LIKE',
                
            );
        }
            
      }
    
    
  • Not sure how this is effecting your code, but your missing a closing } on your one of your loops

    
    foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
        
        // continue if not found in url
        if( empty($_GET[ $name ]) ) {
          
          continue;
          
        }
        
     
       $values = explode(',', $_GET[ $name ]);
    
      // I think there should be a } here before foreach( $values as $value ){
    
      }
    
      // I also think that you should be testing to see if there are any values
    
      if ($values) {
       
    
       $meta_query['relation'] = 'AND';
       foreach( $values as $value ){
        // append meta query
    
          $meta_query[] = array(
                'key'   => $name,
                'value'   => '"' . $value . '"',
                'compare' => 'LIKE',
                
            );
        }
      }
    

    Not sure if any of this will help or not

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Checkbox filter – multiple queries’ is closed to new replies.