Support

Account

Home Forums Front-end Issues Filter post with a custom field in a taxonomy

Solved

Filter post with a custom field in a taxonomy

  • hello everybody. I created a field (checkbox) called ‘location’ in ‘pwb-brand’ taxonomy. I would like to query posts with post type ‘product’ that have taxonomy ‘powb-brand’ with specifit location. i.e. filter products with its brand’s location. I tried several ways that suggested here but I could’nt get the result I want. The field I added is checkbox but I couldn’t get the result even with a simple textfield.

    I tried:

    $taxonomies=get_terms( 'pwb-brand', array(
        'hide_empty' => false, 
        'meta_key' => 'location', // I even tried with 'pwb-brand_905_location' the id stored in database
        'meta_value' => 'paris'    
    ) );
    

    I also tried :

    
    $posts = get_posts(array(
      'post_type' => 'product',
      'numberposts' => -1,
      'tax_query' => array(
        array(
          'taxonomy' => 'pwb-brand',
          'meta_query' => array(
    	array(
    	'key' => 'location', // name of custom field
    	'value' => 'paris', 
    	)
          )   
      )
    )));
    

    Where am I doing it wrong? Can you help me please.

  • After upgrading ACF to version 5, I managed to get it work with the following code but I am not sure whether this is the best approach.

    
    $brands = get_terms(
    	'pwb-brand', array(
    		'hide_empty' => false, 
        		'meta_query' => array(
           		 array(
                		'key' => 'location', 
                		'value' => '"paris"', 
                		'compare' => 'LIKE'
            )
        )
    )); 
    if  ($brands) {
      foreach ($brands  as $brand ) {
        $brandslugs[] = $brand->slug; 
      }
    } 
    $posts = get_posts(array(
     'post_type' => 'product',
     'numberposts' => -1,
     'tax_query' => array(
      array(
       'taxonomy' => 'pwb-brand',
       'field' => 'slug',
       'terms' => $brandslugs
      )
     )
    );
    

    please let me know if there is a better way of doing it.

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

The topic ‘Filter post with a custom field in a taxonomy’ is closed to new replies.