Home › Forums › Feature Requests › Conditional Logic using Taxonomy field
I’ve needed this as well, so I created a custom field setting which allows for conditional logic based on taxonomy. Give it a try when you get a chance.
https://github.com/andrejpavlovic/acf-conditional-logic-advanced
Hi guys!
Is there any news about conditional logic by taxonomy terms?
LOL @ smitxv : did you actually bother to read the topic ? There are 2 custom solutions in this topic and 1 native…
Bee, I’v found two solutions:
1) Plugin: Condition logic advanced: it works with taxonomy selected in WP (tight section) – not acf field
2) Nativ: first field taxonomy term(radio button) and we can select ONE term, then logic – if term id is > and <
But if AFC fiel is multiple selected fied it returns array of terms, what solution?
Thanks!
Good evening, Beee
Do you mean mesaage by marlonleite August 19, 2016 at 10:12 pm
function my_acf_admin_head()… ?
I can not implement it, there is no comments, and I don’t understand how to use it
Or you mean another solution?
Bee, I’v found plugin https://github.com/mattkeys/ACF-Conditional-Taxonomy-Rules it works with multiple select fields, it works!
Pleace help with your code?
I’ve chanched filed name and taxonomy name to save by wp, but it is not work, what is wrong?
function change_post_taxonomy_44582( $post_id ) {
// bail if no ACF data
if ( empty($_POST['acf']) ) {
return;
}
// get term id from $post_id (only 1 value is allowed, so it returns 1 value only)
$stored_sex = wp_get_post_terms($post_id, 'aptype');
// get submitted value from acf form
$posted_sex = $_POST['acf']['acf-field_5c72c3e3cfc4d'];
// get term_id for the submitted value
$term_id = get_term_by( 'name', $posted_sex, 'aptype' );
// if stored value is not equal to posted value, then update terms
if ( $stored_sex != $posted_sex ) {
wp_set_object_terms( $post_id, $term_id->term_id, 'aptype' );
}
}
add_action('acf/save_post', 'change_post_taxonomy_44582', 20);
I can’t tell what’s wrong just from this little piece of code…
It also depends what you wanna do… Just look at the code and dissect it to fit your needs.
Not sure if this has already been discussed, but I actually accomplished this natively using two separate field groups. The first field group applies to my CPT and is a Taxonomy checkbox. Then, I make the field in my second field group required, but I set the LOCATION RULE to only display if my CPT belongs to a specific term (using Post Taxonomy in Location Rule). Seems to work quite well, the only downside is having to deal with two field groups, but it’s a minor organizational inconvenience.
I’ve had the same issue and have created a class if you have multiple post types with fields that you want to appear on whether a term is selected or not.
`<?php
/**
* Class for hiding an acf field based on if a term is selected.
* Developer: Brad Goddard
* Website: https://www.lucid-digital.co.uk
* @package LucidDigital
*/
class Hide_acf_fields_conditional_term {
public $post_type_slug;
public $term_id;
public $field_class;
public function __construct( $post_type_slug, $term_id, $field_class ) {
$this->set_post_type_slug( $post_type_slug );
$this->set_term_id( $term_id );
$this->set_field_class( $field_class );
add_action( ‘acf/input/admin_head’, [$this, ‘my_acf_admin_head’] );
}
/**
* @return mixed
*/
public function get_post_type_slug() {
return $this->post_type_slug;
}
/**
* @param mixed $post_type_slug
*/
public function set_post_type_slug( $post_type_slug ) {
$this->post_type_slug = $post_type_slug;
}
/**
* @return mixed
*/
public function get_term_id() {
return $this->term_id;
}
/**
* @param mixed $term_id
*/
public function set_term_id( $term_id ) {
$this->term_id = $term_id;
}
/**
* @return mixed
*/
public function get_field_class() {
return $this->field_class;
}
/**
* @param mixed $field_class
*/
public function set_field_class( $field_class ) {
$this->field_class = $field_class;
}
function my_acf_admin_head() {
global $post_type;
if ( $this->get_post_type_slug() === $post_type ) :
// Gets taxonomy term from $this->get_post_type_slug().
$taxonomy = get_object_taxonomies($this->get_post_type_slug());
?>
<script type=”text/javascript”>
(function ($) {
acf.add_action(‘ready’, function ($el) {
// Container for the sub title field.
var container = $(‘.’ + ‘<?php echo $this->field_class; ?>’);
// Featured checkbox.
var $field = $(‘#’ + ‘<?php echo $taxonomy[0]; ?>’ + ‘div input’);
// The checkbox relative to $this->get_term_id().
var checkbox = $(‘#in-‘ + ‘<?php echo $taxonomy[0]; ?>’ + ‘-‘ + ‘<?php echo $this->get_term_id(); ?>’);
container.addClass(‘hidden-by-conditional-logic’);
// If $this->get_term_id() term is already selected then show the $this->field_class field.
if ($(checkbox).is(‘:checked’)) {
container.removeClass(‘hidden-by-conditional-logic’);
}
// Click function to show and hide the $this->field_class field.
$field.on(‘click’, function (evt) {
if ($(checkbox).is(‘:checked’)) {
container.removeClass(‘hidden-by-conditional-logic’);
} else {
container.addClass(‘hidden-by-conditional-logic’);
}
});
});
})(jQuery);
</script>
<?php endif;
}
}
new Hide_acf_fields_conditional_term( [post-type-slug], [term-id], [acf-field-container-class] );`
I’ve had the same issue and have created a class if you have multiple post types with fields that you want to appear on whether a term is selected or not.
<?php
/**
* Class for hiding an acf field based on if a term is selected.
* Developer: Brad Goddard
* Website: https://www.lucid-digital.co.uk
* @package LucidDigital
*/
class Hide_acf_fields_conditional_term {
public $post_type_slug;
public $term_id;
public $field_class;
public function __construct( $post_type_slug, $term_id, $field_class ) {
$this->set_post_type_slug( $post_type_slug );
$this->set_term_id( $term_id );
$this->set_field_class( $field_class );
add_action( 'acf/input/admin_head', [$this, 'my_acf_admin_head'] );
}
/**
* @return mixed
*/
public function get_post_type_slug() {
return $this->post_type_slug;
}
/**
* @param mixed $post_type_slug
*/
public function set_post_type_slug( $post_type_slug ) {
$this->post_type_slug = $post_type_slug;
}
/**
* @return mixed
*/
public function get_term_id() {
return $this->term_id;
}
/**
* @param mixed $term_id
*/
public function set_term_id( $term_id ) {
$this->term_id = $term_id;
}
/**
* @return mixed
*/
public function get_field_class() {
return $this->field_class;
}
/**
* @param mixed $field_class
*/
public function set_field_class( $field_class ) {
$this->field_class = $field_class;
}
function my_acf_admin_head() {
global $post_type;
if ( $this->get_post_type_slug() === $post_type ) :
// Gets taxonomy term from $this->get_post_type_slug().
$taxonomy = get_object_taxonomies($this->get_post_type_slug());
?>
<script type="text/javascript">
(function ($) {
acf.add_action('ready', function ($el) {
// Container for the sub title field.
var container = $('.' + '<?php echo $this->field_class; ?>');
// Featured checkbox.
var $field = $('#' + '<?php echo $taxonomy[0]; ?>' + 'div input');
// The checkbox relative to $this->get_term_id().
var checkbox = $('#in-' + '<?php echo $taxonomy[0]; ?>' + '-' + '<?php echo $this->get_term_id(); ?>');
container.addClass('hidden-by-conditional-logic');
// If $this->get_term_id() term is already selected then show the $this->field_class field.
if ($(checkbox).is(':checked')) {
container.removeClass('hidden-by-conditional-logic');
}
// Click function to show and hide the $this->field_class field.
$field.on('click', function (evt) {
if ($(checkbox).is(':checked')) {
container.removeClass('hidden-by-conditional-logic');
} else {
container.addClass('hidden-by-conditional-logic');
}
});
});
})(jQuery);
</script>
<?php endif;
}
}
new Hide_acf_fields_conditional_term( [post-type-slug], [term-id], [acf-field-container-class] );
can’t find those settings mentioned here:
https://www.advancedcustomfields.com/blog/acf-pro-5-7-0-new-architecture-new-features/
does anyone got this to work native?
without multiple field groups. just conditional logic based on taxonomy field value?
The plugin Smitxv mentions above : https://github.com/mattkeys/ACF-Conditional-Taxonomy-Rules is exactly what I needed. Allowing conditional based on chosen Taxonomy ID. Maybe that should be added to future release of ACF.
Thanks.
Hi folks, I know this has been a long time coming – but it’s here 🎉
We’ve recently shipped ACF 6.3 with improved conditional logic. This means you can actually select terms of a Taxonomy field when defining conditional logic rules!
It works for the Taxonomy, User, Post Object, Page Link, and Relationship fields:
https://www.advancedcustomfields.com/blog/acf-6-3-0-released/#conditional-logic-relational-fields
The topic ‘Conditional Logic using Taxonomy field’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.