The field name is “div2” that is not the taxonomy.
What is the taoxnomy?
I’m attaching an image of the custom field configuration.
There are posts with that taxonomy, that’s my concern about it.
Well, if you’ve created a custom taxonomy then that value must be the value of your custom taxonomy. Other than this I see no reason it should not be working unless there are no posts of that post type that are in that term of the taxonomy.
'taxonomy' => 'div2',
To set the parent post from the front end you need a relationship field that is only available on the front end. Set this up to start. Add a post object field that only allows one selection.
Create the following filters for your field in functions.php
// only show field on front of site
// https://www.advancedcustomfields.com/resources/acf-prepare_field/
add_filter('acf/prepare_field/name=YOUR FIELD NAME HERE', 'field_only_on_front');
function field_only_on_front($field) {
if (is_admin()) {
return false;
}
return $field;
}
// alter the acf query of the post object field to only show top level posts
https://www.advancedcustomfields.com/resources/acf-fields-post_object-query/
add_filter('acf/fields/post_object/query/name=YOUR FIELD NAME HERE', 'only_top_level_posts', 20, 3);
function only_top_level_posts($args, $field, $post_id) {
$args['post_parent'] = 0;
return $args;
}
After you do this set up your taxonomy field to return term objects
Once all that is done then then create an acf/save_post filter in functions.php
add_action('acf/save_post', 'update_and_save_journal_post');
function update_and_save_journal_post($post_id) {
// make sure to only do this on the right post type
$post_type = get_post_type($post_id);
if ($post_type != 'YOUR POST TYPE HERE') {
return;
}
// get the post
$post = get_post($post_id);
// get the parent, unformatted, ID only
$parent = get_field('YOUR POST OBJECT FIELD', $post_id, false);
if ($parent ) {
$post->post_parent = $parent;
}
// get the taxonomy field
$terms = get_field('YOUR TAXONOMY FIELD;, $post_id);
if (!empty($terms) && !is_wp_error($terms)) {
$term = terms[0];
$post->post_title = $term->name.$post->post_title;
}
// remove this action so that an infinite loop is not created
remove_filter('acf/save_post', 'update_and_save_journal_post');
// update the post
wp_update_post($post);
// re-add this filter
add_action('acf/save_post', 'update_and_save_journal_post');
}
Please note that none of the above code has been tested for syntax errors.
Oh I see. I set it using the WO taxonomy checkboxes which appear in the right column. I am not using the ACF taxonomy selector.
Thanks
The price field is on the taxonomy term. Before you can get that field you need to get the term to get it from.
I am asking how you set this term when you edit a “Repair” post.
Hi,
I don´t really understand. I set up the taxonomy using CPT UI and then added a ACF field to it for the price field. Everything else is standard. I am not using the ACF taxonomy type field.
Before I answer the entire question. What is the return value of the taxonomy field set to? Term ID or Object?
Do you have a field where you set the term in each of these taxonomies or do they use the standard WP metaboxes?
You need to get the term in the taxonomy related to the post, what you need to do that depends on if you have an ACF taxonomy field for each taxonomy on the post edit page or not.
Just adding this here in case someone else needs this. This piece of code will add the first taxonomy term as default for all taxonomy fields *on the frontend*. I needed this for frontend forms, but feel free to remove the “is_admin” check if that’s what you need.
Beware of potential overhead if you have many terms or fields that would be triggered by this.
function set_tax_default($field) {
if(!is_admin()){
$terms = get_terms($field['taxonomy'], ['number' => 1]);
if($terms) {
$single_term = reset($terms);
$field['default_value'] = $single_term->term_id;
}
}
return $field;
}
add_filter('acf/load_field/type=taxonomy', 'set_tax_default');
This worked for me to get the taxonomy terms for the current post with an ACF icon field attached:
<?php $terms = get_the_terms( get_the_ID(), 'project_services' );
$cat_icon = get_field('icon', $queried_object);
?>
<?php if( $terms ): ?>
<ul class="menu">
<?php foreach( $terms as $term ): ?>
<li>
<a href="<?php echo get_term_link( $term ); ?>">
<?php $icon = get_field('icon', $term->taxonomy . '_' . $term->term_id);?>
<img src="<?php echo $icon ?>">
<?php echo $term->name; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Hello, thanks for the answer, but I had something else in mind.
There is a function in acf-form-functions.php
function acf_form_data( $data = array() ) {
// Apply defaults.
$data = wp_parse_args($data, array(
/** @type string The current screen (post, user, taxonomy, etc). */
'screen' => 'post',
/** @type int|string The ID of current post being edited. */
'post_id' => 0,
/** @type bool Enables AJAX validation. */
'validation' => true,
));
In which the value “validation” is always = true.
Is it possible to change this value to “false” when creating one form, and leave it “true” when creating another?
Thankyou.
Hi guys, firstly thank you for providing your solution to this – I’m trying to do the same thing but I need to use an ACF field’s related taxonomy in the Post Title…
When I implement your code @andycheeseman I can pull in the ID of the field I want, but not the Object. I have tried setting the Return Value in the field’s settings to Term Object rather than Term ID but I still get the ID.
For reference this is the code I am using:
function my_post_title_updater( $post_id ) {
if ( get_post_type() == 'match' ) {
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_title'] = get_field( 'home_team', $post_id );
// Update the post into the database
wp_update_post( $my_post );
}
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_post_title_updater', 20);
`
Any help on how to get the taxonomy values from an ACF field would be much appreciated!!
Thanks 🙂
Under Locations, select the Taxonomy Term rule and choose the corresponding value to show this field group.
What type of fields? Relationship? Post Object? Taxonomy?
What are Wines? Grapes? Posts? Terms?
For those interested in the feature, here’s how it worked for me.
function.php
// Loop Gallery Cat1
function my_custom_gallery_cat1() {
query_posts(array(
'post_type' => 'gd_project',
'orderby' => 'rand',
'posts_per_page' => 10,
'tax_query' => array(
array (
'taxonomy' => 'gd_projectcategory',
'field' => 'slug',
'terms' => 'example-terms',
)
),
));
?> <div class="masonry">
<?php
while (have_posts()) : the_post();
$image_field = get_field('image');
$image_url = $image_field['url'];
$image_alt = $image_field['alt']; ?>
<div class="grid-item">
<a href="<?php echo get_permalink( $post->ID ); ?>" target="_blank">
<img src="<?php echo esc_url($image_url); ?>" alt="<?php echo esc_attr($image_alt); ?>" />
<h4><?php the_field( 'title' ); ?></h4>
</a>
</div>
<?php endwhile; ?>
</div> <?php
}
add_shortcode('example-terms', 'my_custom_gallery_cat1');
single-example.php
<div class="container-fluid">
<div class="row">
<?php echo do_shortcode('[categoria]');?>
</div>
</div>
Each taxonomy option is documented in detail in the WordPress Codex. After adding this to your theme’s functions.
How to get fields set in the Taxonomy List location?
Hi Jared,
Thank you very much for taking the time to help me out!
I appreciate your code too!
In fact, I have created a taxonomy for ‘Listing-Location’ for my Custom post type, (created with CPT plugin).
I know enough to change the slugs of your code but if you could kindly clarify, where would I place this PHP code so it displays in my section?
On the taxonomy code, (provided within the CPT plugin interface)?
That’s my trouble…not sure where to implement it!
Thank you again and if it helps, I’m trying to achieve something very similar to the example
at the section titled: ‘Explore Cities’ towards the end – they show for example Chicago: 13 properties.
Well, I’m trying to get this dynamically to show for my locations!
Thank you!!!
Hi!
I think I understand, you’re probably trying to query by a custom field.
Looking at Example 1. from that resource page, it’s very similar to what you are trying to accomplish. Assuming your custom field is attached to some kind of query-able post type like posts
or pages
(my preference is to create a custom post type) this should be very easy.
<?php
// args
$args = array(
'posts_per_page' => -1, //get all posts
'post_type' => 'your_post_type_slug', //posts, pages, whatever the field is attached to
'meta_key' => 'listing-location', //custom field slug
'meta_value' => 'New York' //location to count
);
// query
$the_query = new WP_Query( $args ); //get WP Query using $args
$count = $the_query->post_count; //use property post_count to return the count
echo 'Location: New York';
echo 'Total listings I have with that location are '. $count;
?>
The property post_count
is available when using a WP_Query. More information here: WP_Query
Obviously this may not be the most efficient code because the location is hard coded, but maybe that works in your situation.
My advice would be to set up the listing location as a custom category/taxonomy instead of an ACF field. That way you could loop through all the available categories dynamically.
Hope this helps!
I have a website which is going to be recommending books.
I want a custom field where I enter the Amazon URL, and I get back the following data automatically:
Book title (to be set as post title) Book author (to be added to ‘Authors’ custom taxonomy) Book image (to be set as featured image) Book genre (to be added to ‘Genres’ custom taxonomy) Book description (to be added to custom field)
Is there a way to achieve this with ACF Pro? I thought maybe oEmbed would be the way to go about it, but I’m not having a lot of luck there.
Does anyone have any suggestions?usps tracking
‘tax_query’ => array(
array(
‘taxonomy’ => ‘references-cats’,
‘field’ => ‘slug’,
‘terms’ => $slug
)
)
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.