$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>advanced custom fields - How to get_terms() only of terms matching an ACF meta value?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

advanced custom fields - How to get_terms() only of terms matching an ACF meta value?

matteradmin10PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far