2

My custom taxonomy "company" has an Advanced Custom Fields field group with fields including a Checkbox, "tags". (This adds to each "company" term fields including "tags", which accepts multiple string values corresponding to checkbox items).

I need to gather several groups of terms whose "tags" values include particular distinct values, eg. "Publishing", "Media", "Advertising.

So far, I have the following method, in which I cycle through all "company" terms and, if the string is found, add that term's ID to an array, used later to get actual term objects...

// Pick a focus tag (choose from 'Tags' ACF Checkbox values in the Organisation field)
$tag_section = "Publishing";

// First, get all organisation terms
$organisations = get_terms( 'company' );

// Initialise array we will use to store only matching organisations
$orgs_filtered = array();

// Make an array with only IDs of organisations with a matching tag
foreach ($organisations as $organisation) {
   // Construct prefixed object ref for this organisation
   $org_id_prefixed = 'company_'. $organisation->term_id;
   // If the intended value matches the target value
   if(in_array($tag_section, get_field('tags', $org_id_prefixed))){
      // add its term ID to our array - use this array to feed a WP_Query or similar
      array_push($orgs_filtered, $organisation->term_id);
   }
}

... But this only gives me an array of the IDs of matching terms - not an array **of the terms* themselves.

It feels like my method is cumbersome, and that it should be possible to make the above more efficient and get the actual term objects themselves.

I have read about using meta_query with get_posts via ACF and I am familiar with WordPress' get_terms(). So I am hopeful it is possible to use a similar method.

But, when I try the following test in pursuit, it returns nothing from $my_terms...

$args = array(
    'taxonomy'   => 'company',
    'hide_empty' => false,
    'meta_query' => array(
         array(
            'key'       => 'tags',
            'value'     => 'Publishing',
            'compare'   => '='
         )
    )
);
$my_terms = get_terms($args);

echo '<pre>';
print_r($my_terms);
echo '</pre>';

Thinking perhaps the 'value' should be an array, I changed the value part to 'value' => array('Publishing'), ...

But then that just shows ALL "company" terms from the site, with no matching in effect.

I have also tried both variations with a compare value of in.

Do you have any ideas about this?

Thanks.

  • The last example on the checkbox documentation page you linked above shows how to use a meta query. It has to be a LIKE comparison because it's stored as a serialized array. – Milo Jan 2 '19 at 0:15
  • Ah-hah, yes, the formulation is... ` array( 'key' => 'tags', 'value' => 'Publishing', 'compare' => 'LIKE' )` – Robert Andrews Jan 2 '19 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.