Support

Account

Home Forums Front-end Issues Filter by field name

Solved

Filter by field name

  • Hello to all, I have been using ACF Pro for a while, and it’s one of the best WP plugins out there.

    Now I have one issue, regarding the filter code you guys provided here:
    https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/

    The only thing I modified in your code is the field names, for example:

        $GLOBALS['my_query_filters'] = array( 
            'field_1'	=> 'event_type', 
            'field_2'	=> 'event_fee_structure',
            'field_3'	=> 'event_country'
        );

    I’ve been using it to filter trough events on my site, but now i have a small issue with one particular filter.

    The code it self works if there is only one value in the field.

    For example, if the field name is “event_country”, and the field value is “Australia”, when I filter it trough url http://www.mysite.com/?event_country=Australia, it gives me correct result.

    The issue I’m having is if there is two values in the field, either in a string (Australia, Germany) or in an array ([“Australia”, “Germany”]), it doesn’t give me anything if I search only for one of them, eg. http://www.mysite.com/?event_country=Australia.

    I’ve been trying to find a solution to this for a while now, but couldn’t.
    Any help would be appreciated. Thanks in advance.

  • You need to change the 'compare' to 'LIKE' and the value to look to '"'.$value.'"'

    See 3. Multiple custom field values (array based values) on this page https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

  • No, i tried that already.
    The values are stored by a multi-select field, so it gives an array.

    I tried with:

            $meta_query[] = array(
                    'key'		=> $name,
                    'value'		=> '"'.$value.'"',
                    'compare'	=> 'LIKE'
            );

    also tried to change the ‘key’ manually to only one value (“event_country”), and tried with the solution you gave me above (3. Multiple custom field values), by modifying code like this :

    ‘meta_query’ => array(
    ‘relation’ => ‘OR’,
    array(
    ‘key’ => $name,
    ‘value’ => ‘”‘.$value.'”‘,
    ‘compare’ => ‘LIKE’
    ),
    array(
    ‘key’ => $name,
    ‘value’ => ‘”‘.$value.'”‘,
    ‘compare’ => ‘LIKE’
    )
    )

  • Have you recently changed the acf field in question form a single select field to a multi select field? Did you change it after data was added to the field while it only allowed one value and then change it to allow multiple selections?

  • Yes, the field was changed multiple times, from single select field, to multi select field, and to plain text field.

    Every time I changed the field, I updated the values again, so it would overwrite the old ones.

  • The only thing that I can think of here is that some of your posts contain single values and some contain arrays an possibly forms of values and not all the values have been updated.

    You need to look, I’d look in the database and see what’s what and make sure I got everything updated.

    as a work-around, but it will reduce query efficiency, you can use nested meta queries and check for a value either being exactly == or LIKE for each field.

    
    'meta_query' => array(
      'relation' => 'OR',
      array (
        'relation' => 'OR',
        array(
          'key' => 'field_name_1',
          'value' = $value_1
       ),
        array(
          'key' => 'field_name_1',
          'value' = '"'.$value_1.'"'
          'compare' => 'LIKE'
        )
      ),
      array (
        'relation' => 'OR',
        array(
          'key' => 'field_name_2',
          'value' = $value_2
       ),
        array(
          'key' => 'field_name_2',
          'value' = '"'.$value_2.'"'
          'compare' => 'LIKE'
        )
      )
    )
    
  • John, thank you for your help, i figured out what the problem was, after I used your meta_query code, it didn’t work, but then I started to do a bit more research, and it turns out the problem was in the $value var.

    I changed
    $value = explode(',', $_GET[ $name ]);

    to $value = $_GET[ $name ] and it worked flawlessly.

    To help anyone else, my whole code was:

    function lang_pre_get_posts( $query ) {
    
        // array of filters (field key => field name)
        $GLOBALS['my_query_filters'] = array( 
            'field_1'	=> 'event_country'
        );
    	
    	// 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 ) {
    		
    		// continue if not found in url
    		if( empty($_GET[ $name ]) ) {
    			continue;
    		}
    
    		if( ! $query->is_post_type_archive() ) return;
    		
    		// get the value for this filter
    		$value = $_GET[ $name ];
    		
    		// append meta query
            $meta_query[] = array(
              'relation' => 'OR',
                array(
                  'key' => 'event_country',
                  'value' => '"'.$value.'"',
                  'compare' => 'IN'
    
               ),
                array(
                  'key' => 'event_country',
                  'value' => '"'.$value.'"',
                  'compare' => 'LIKE'
                ),
    
            );
            
    	} 
    
    	// update meta query
    	$query->set('meta_query', $meta_query);
    }
    
    add_action('pre_get_posts', 'lang_pre_get_posts', 10, 1);
Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Filter by field name’ is closed to new replies.