Support

Account

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

  • 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.