thanks, assuming i know the taxonomy ids , could it be like this ?
function my_acf_save_post_function($post_id) {
$condition = get_field('first_tax_field', $post_id, false);
// get the value checked by user
if ($condition=='36') {
update_field('second_tax_field', '78', $post_id);
}
else {
update_field('second_tax_field', '79', $post_id);
}
}
Hi John Huebner,
I am using ACF Pro 5.6.10 with WP 5.1.1 and using Mulitsite.
I have some other ACF plugins installed as listed below,
Advanced Custom Fields: Advanced Taxonomy Selector
Advanced Custom Fields: Font Awesome
Advanced Custom Fields: Gravityforms Add-on
Advanced Custom Fields: Image Crop Add-on
Advanced Custom Fields: Limiter
Advanced Custom Fields: Link Picker
Advanced Custom Fields: Post Type Selector
Advanced Custom Fields: Widget Area Field
Any help would be highly appreciated.
Thanks
thanks i am going to try it.
this the related post
https://support.advancedcustomfields.com/forums/topic/update_field-for-taxonomy-field-type/
Can you give me a link to this other question I answered?
At this point I can only say what I would do.
When adding the taxonomy field in ACF there are settings for saving and loading terms, I would turn both of these on.
function my_acf_save_post_function($post_id) {
// get the value of the first taxonomy field
// I am getting it unformulated
// which means that it's returning an array of term IDs
// you might do this differently depending on what you want to do
get_field('other_tax_field', $post_id, false);
// check this field for whatever condition you are looking for
if ($condition) {
// get the value of the field you want to change
$other_field = get_field('the other field', $post_id, false);
// append a term ID value to the value
// if it does not already exist
if (!in_array($new_term_id, $other_field)) {
$other_field[] = $new_term_id;
}
// update acf field
// acf will take care or updating terms because of the settings
update_field('the other field', $other_field, $post_id);
}
}
1) Are these taxonomy fields in a repeater?
NO
2) Is your ACF field you want to update set up to “Load Terms” and “Save Terms”?
Not sure , YES i suppose
3) Do you want to save the values in just the ACF field or do you want to Save/Add these values Terms for the post in WP?
For the POST in WP
That depends on several things.
1) Are these taxonomy fields in a repeater?
2) Is your ACF field you want to update set up to “Load Terms” and “Save Terms”?
3) Do you want to save the values in just the ACF field or do you want to Save/Add these values Terms for the post in WP?
Actually i have 2 ACF taxonomy radio fields in a form in ACF save post.
First is visible, second is hidden.
I would like to get the value (ID) of the first taxonomy field checked by user.
Depending on this value, i want to set the value of the second field and save it to the database.
i tought i had to use get_terms and wp_set_object_terms
Thanks, that’s interesting. I thought I was generating a new taxonomy with the function above. I looked around and I ended up finding https://wordpress.org/plugins/acf-advanced-taxonomy-selector/ which allows me to use specific categories for the CPT and not have them appear anywhere else, and which works with the function above. So that’s what I was looking for.
And wsing'meta_box_cb' => false
does remove the category metabox while leaving the menu item in admin, and works with the plugin and function above.
In the post template you would get the category of the post and then get the image field from the category and display that instead of the post’s featured image. https://www.advancedcustomfields.com/resources/taxonomy/
To hide the standard WP meta box add this setting to the taxonomy, https://codex.wordpress.org/Function_Reference/register_taxonomy
'meta_box_cb' => false,
When setting up a taxonomy field there is a setting to choose a taxonomy you are using.
If you are using the built in Category taxonomy then there isn’t any way to limit the terms to just those used on your CPT. And there isn’t any way to prevent people from selecting these categories for post. If you need terms that can only be used for your CPT then you need to create a new taxonomy for this. That’s just the way WP works.
I’ve actually stunned myself and somehow figured this out. See my messy code below.
Each category has an image assigned in ACF (prod_image). I created a group “Taxonomy is equal to Category”, field type “Taxonomy” with radio buttons outputting a term ID.
<?php
$the_cat = get_the_category();
$the_cat = get_the_category( $id );
$category_name = $the_cat[0]->cat_name;
$category_link = get_category_link( $the_cat[0]->cat_ID );
?>
<!--Related Games-->
<?php if( get_field('show_related_games', 'term_'. $cat_id) ): ?>
<div>
<!--Section Title-->
<h3>
<?php the_field( 'related_games_title', 'term_'. $cat_id ); ?>
</h3>
<!--Repeat field-->
<?php if( have_rows('related_game', 'term_'. $cat_id) ): while ( have_rows('related_game', 'term_'. $cat_id) ) : the_row(); ?>
<!--Product-->
<div>
<?php $term_id = get_sub_field('related_link');
if( $term_id):
$term_name = get_cat_name( $term_id ) ;
$term_url = get_category_link( $term_id );
$prod_image = get_field( 'prod_image', 'term_'. $term_id);?>
<a href="<?php echo $term_url; ?>" class="relatedprod" target="_blank">
<!--Packshot-->
<?php if ( $prod_image ) { ?>
<img src="<?php echo $prod_image['url']; ?>" alt="<?php echo $prod_image['alt']; ?>" />
<?php } ?>
<!--Product Info-->
<div class="prodinfo">
<div class="gamename">
<?php echo $term_name; ?>
</div>
<div class="smallgreencta">
<?php the_sub_field('cta_text'); ?>
</div>
</div>
</a>
<?php endif; ?>
</div>
<?php endwhile; else :
// no rows found
endif; ?>
</div>
<?php endif;?>
Yes, I saw that. I must not have used it correctly. If you don’t mind looking at it the current code is below> Thanks again.
<?php $args = array(
‘post_type’ => ‘properties’,
‘tax_query’ => array(
array(
‘posts_per_page’ => -1,
‘taxonomy’ => ‘property_type’,
‘field’ => ‘slug’,
‘terms’ => ‘retail’
),
),
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
$term = get_queried_object();
$property_address = get_field(‘property_address’,$term);
echo $property_address;
?>
<?php the_title();?>
<h2 class=”prop-address”><?php the_field(‘property_address’); ?></h2>
<p class=”prop-description”><?php the_field(‘property_description’); ?></p>
<?php endwhile; ?>
Hey HimSelf, thanks for your help on this.
Sorry, but I don’t know how to add a $term to the custom field. Some of the articles I’ve viewed show a Taxonomy Term option in the Field Type dropdown, but my version of ACF does not have that option (see attached screencapture).
I did notice a ‘Create Terms’ toggle at the bottom of that screenshot, but cannot find documentation on how to use it. Any advice?
Thank you again for your reply. I really appreciate your help.
Since this topic still rates highly in Google results, just thought I would chime in with a really simple way to do this using the Javascript API.
The ACF Javascript API has a filter ‘select2_ajax_data’ which allows us to send extra data with the AJAX request which gets the list of terms for us to choose from.
We can use this along with the PHP filter ‘acf/fields/taxonomy/query’ to do our dynamic filtering.
The idea is simple enough. Each time we click the ACF taxonomy multiselect field to make a selection:
– If we have not selected any terms yet, we want to see top-level terms (parent = 0)
– If we have selected one or more terms already, we only want to see children of the most recent term selected.
The most fragile bit of this is the jQuery which finds the most recent term selected – you may have to tweak this in future.
The code:
/* Javascript / jQuery */
acf.add_filter('select2_ajax_data', function( data, args, $input, field, instance ){
var target_field_key = 'field_5c7634ca3413f'; //YOUR TARGET FIELD KEY HERE
if(data.field_key == target_field_key){
var field_selector = '[name="acf[' + target_field_key + '][]"]'; //the select field holding the values already chosen
if($(field_selector).val() != '' && $(field_selector).val() != null){
var collections = $(field_selector).val();
parent_id = collections.pop(); //parent of available options will be set to the last term selected
}
else{
parent_id = 0; //nothing chosen yet, offer only top-level terms
}
data.parent = parent_id;
}
return data;
});
/* PHP */
function custom_acf_taxonomy_hierarchy( $args, $field, $post_id ){
$args['parent'] = empty($_POST['parent']) ? 0 : $_POST['parent'];
return $args;
}
add_filter('acf/fields/taxonomy/query/key=field_5c7634ca3413f', 'custom_acf_taxonomy_hierarchy',10,3);
if i understand ACF taxonomy uses SELECT2 JS library and in that allows multiple options (hierarchical, bold etc…) Possible to setup these optinos ?
@waterlemon Thank you for sharing your thoughts and final solution!
I have exactly the same issue (I want to display an unique SEO description below my product feed on every woocommerce category site) but unfortunately your solution did not succeed so far.
Could you – or @ma4ine – please specify a little more what you did:
The changes were made in the taxonomy-product_cat.php or in archive-product.php?
I tried both (incl. replacing the field name, of course) but nothing happend…
First, I copied the original files and uploaded the changed files then to yourtheme/woocommerce/… to overwrite the original files (/plugins/woocommerce/templates/…). Maybe that is not the right way to do it?
Would be very thankful to get more information on that. Unfortunately I have very few experience on programming etc. and trying to get that done for many hours now, puh… 😉
Would be awesome, thanx!!
Hi Jhon,
appreciate the reply! The code above is coming from woocommerce/content-single-product.php
Basically there is an ACF field called featured_products that is displayed on the each product page while creating/editing a product. This would be so we can select what products we want to feature for the current product.
With your help I have resolved the issue in the code previous developer had done.
I amended the code and left out the following:
<!-- php
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DSC',
'posts_per_page' => 4,
'meta_query' => $meta_query,
'tax_query' => $tax_query,
);
-->
Then edited the code below this to reflect
<?php
$post_objects = get_field( 'featured_products' );
if( $post_objects ): ?>
while adjusting the code within the divs.
THANK YOU SO MUCH! I would never have gotten this without your suggestion (and some heavy reading in the documentation!)
The code is working exactly as you describe and it seems that is what it is meant to do.
The first part
<?php
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DSC',
'posts_per_page' => 1,
'meta_query' => $meta_query,
'tax_query' => $tax_query,
);
?>
<div class="row">
<?php
$featured_query = new WP_Query( $args );
This is getting a 1 product that has the “visibility” taxonomy term of “feature_product” set. This will always get the same post. The the rest of the code is getting the featured product as set on this product.
I don’t know where your code is running or in what template, but the above code should likely be removed and somewhere in the single product template you should be getting the fields from the current product being shown rather than the above. The code you posted would be useless for doing this and if that’s not what should be happening should probably all be removed.
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);
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!
Hi guys!
Is there any news about conditional logic by taxonomy terms?
To be honest, this does not really have to deal specifically with advancedcustomfields. Each of these elements can be found in WordPress documentation. Considering you use a taxonomy field.
https://codex.wordpress.org/Function_Reference/get_taxonomy
With big thanks to Elliot for helping me figure this out, and for the benefit of anyone else who finds this, the main problem was wrapping acf.Condition
in jQuery on ready.
For testing purposes, this code works and can be added to a theme’s functions.php:
add_action( 'acf/admin_footer', 'my_admin_footer' );
function my_admin_footer() {
?>
<script>
(function( $ ) {
'use strict';
var HasTerm = acf.Condition.extend({
type: 'hasTerm',
operator: '==hasTerm',
label: acf.__('Has term'),
fieldTypes: [ 'taxonomy' ],
match: function( rule, field ){
return rule.value === field.val();
},
choices: function( fieldObject ){
return '<input type="text" />';
}
});
acf.registerConditionType( HasTerm );
}( jQuery ));
</script>
<?php
}
Or wait wait wait… did you mean “tag” like the wordpress taxonomy? Because, if so, that’s a completely different query.
I suppose some cautions are in order:
*) never cache if you can’t measure the difference in performance (you won’t know if you are making things better or worse).
*) writing code for a wordpress theme tends to be different from writing code for a wordpress plugin.
*) if you’re doing this on a “multisite” (or have a strange configuration) that would also change the details of the query.
*) the code snippet I posted was intended to be in the body of a php function.
*) if you need this to be a part of the $wp_query, that introduces other issues (including things like the url structure that this fits into).
Anyways, if this doesn’t work, I would have to ask you for more information about what specifically you’re doing.
(And, if you’re using wordpress ‘tags’ (also known as the ‘term’ taxonomy), then maybe you should be doing some other things different. Like maybe it makes sense to have a video custom post type and have the tags on the videos rather than buried deep in the artist description?)
Hi. I just purchased this pro version. So far so good.
But this tutorial on step #3 when trying to add a field stump me. I also can’t see “Taxonomy Term” under Locations.
I disabled all plugins and tried another wordpress package theme, no luck. 🙁
Can it be because I am working locally?
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.