Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

ACF: Filter Terms by Custom Field? WordPress

  • SOLVED

I'm using the Advanced Custom Fields plug-in. I have a custom 'select' field applied to a taxonomy within a custom post type. I would like to list the terms (not posts) based on whether the custom field value is set to 'Active' or 'Not Active' on the term. Basically, I need the code to pull terms set to "active" only.

Below is what I have so far, but alas it's not working. The code displays my terms, but it's not filtering them by the field value, so I think I'm doing it wrong.

Was trying to follow [[LINK href="http://www.advancedcustomfields.com/resources/code-examples/"]]this page's[[/LINK]] instructions near the bottom, then apply it to terms instead of posts, but I guess I'm not doing it correctly.

<?php

$terms = get_terms('focus15', array(
'meta_key' => 'group_active_in_focus_15',
'meta_value' => 'active15',
'exclude' => array(16, 20, 22, 25, 27, 28, 30, 4, 33 ), //* Enter ID's of parent categories to exclude from list
'hide_empty' => 0,
'numberposts' => -1,

));

if($terms)
{
echo '<ul>';

foreach($terms as $term)
{
echo '<li>' . $term->name . '</a></li>';
}

echo '</ul>';
}

?>


UPDATE: I added a screenshot of my custom field settings in Dashboard.

Answers (4)

2014-08-26

Arnav Joy answers:

please try this

<?php

$terms = get_terms('focus15', array('exclude' => array(16, 20, 22, 25, 27, 28, 30, 4, 33 ),'hide_empty' => 0));

if($terms)

{

echo '<ul>';



foreach($terms as $term)

{

if( get_field('group_active_in_focus_15', 'focus15_'.$term->term_id) != 'active15' )
continue;

echo '<li>' . $term->name . '</a></li>';

}



echo '</ul>';

}


you can read more about it here

http://www.advancedcustomfields.com/resources/how-to-get-values-from-a-taxonomy-term/


streetfire comments:

Arnav Joy, This is working great! How is it I never found that link before, *sigh*. Thank you for your solution :)


Arnav Joy comments:

please try this one.

<?php

$terms = get_terms('focus15', array('exclude' => array(16, 20, 22, 25, 27, 28, 30, 4, 33 ),'hide_empty' => 0));

if($terms)

{

echo '<ul>';



foreach($terms as $term)

{

if( get_field('group_active_in_focus_15', 'focus15_'.$term->term_id) != 'active15:Active' )
continue;

echo '<li>' . $term->name . '</a></li>';

}



echo '</ul>';

}


Arnav Joy comments:

Good to hear that ! Please vote me and close the question .

Thanks
Arnav


streetfire comments:

I have! Thanks :)

2014-08-26

Luis Abarca answers:

Can you copy and paste the custom field content ??

meta_key and meta_value are not valid arguments of [[LINK href="http://codex.wordpress.org/Function_Reference/get_terms"]]get_terms[[/LINK]]


streetfire comments:

Luis, are you referring to the values?


Luis Abarca comments:

Yeah, the values of your custom field please.

2014-08-26

zebra webdesigns answers:

..