Hey dude!
I think that if you want to do this you’d have to go into the relationship field file and add the functionality. It shouldnt be too hard since Elliot has already added a filter by post type..
If you want it to stay “clean” you can copy the field to your theme, rename it and do the changes there and thus creating a custom relationship field beside the regular one.
Hi @seamusleahy
Thanks for the feedback. This will be added in a future update.
Cheers
Elliot
Let’s start by trying include the PayPal field last.
// custom action for saving the front end form
function my_pre_save_post( $post_id )
{
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id;
}
// Create a new post
$post = array(
'post_status' => 'draft' ,
'post_title' => $_POST['title'],
'tax_input' => array('job-category' => $_POST['job-category']),
'post_type' => 'jobs_post' ,
);
// insert the post
$post_id = wp_insert_post( $post );
// now set the taxonomy
wp_set_object_terms($post_id, array($_POST['job-category']), 'job-category');
// wp_set_object_terms($post_id, array($_POST['salary-range']), 'salary-range');
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
//Rename Options Menu ACF
function my_acf_options_page_settings( $settings )
{
$settings['title'] = 'Theme Options';
$settings['pages'] = array('Email', 'Google AdSense', 'Google Analytics', 'Logo', 'Theme Colors');
return $settings;
}
add_filter('acf/options_page/settings', 'my_acf_options_page_settings');
//end
include_once('add-ons/advanced-custom-fields/acf.php');
include_once('add-ons/acf-location-field-master/acf-location.php');
include_once('add-ons/acf-repeater/acf-repeater.php');
include_once('add-ons/acf-options-page/acf-options-page.php');
include_once('add-ons/acf-taxonomy-field-master/taxonomy-field.php');
require ( get_template_directory() . '/add-ons/custom-widgets.php' );
require ( get_template_directory() . '/add-ons/custom-form.php' );
//ACF PayPal
add_action('acf/register_fields', 'my_register_fields');
function my_register_fields()
{
include_once('add-ons/acf-paypal-field-master/paypal_item-v4.php');
}
Thanks Mike, I appreciate it! Here is all the code in my functions.php relating to acf:
// custom action for saving the front end form
function my_pre_save_post( $post_id )
{
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id;
}
// Create a new post
$post = array(
'post_status' => 'draft' ,
'post_title' => $_POST['title'],
'tax_input' => array('job-category' => $_POST['job-category']),
'post_type' => 'jobs_post' ,
);
// insert the post
$post_id = wp_insert_post( $post );
// now set the taxonomy
wp_set_object_terms($post_id, array($_POST['job-category']), 'job-category');
// wp_set_object_terms($post_id, array($_POST['salary-range']), 'salary-range');
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
//ACF PayPal
add_action('acf/register_fields', 'my_register_fields');
function my_register_fields()
{
include_once('add-ons/acf-paypal-field-master/paypal_item-v4.php');
}
//Rename Options Menu ACF
function my_acf_options_page_settings( $settings )
{
$settings['title'] = 'Theme Options';
$settings['pages'] = array('Email', 'Google AdSense', 'Google Analytics', 'Logo', 'Theme Colors');
return $settings;
}
add_filter('acf/options_page/settings', 'my_acf_options_page_settings');
//end
include_once('add-ons/advanced-custom-fields/acf.php');
include_once('add-ons/acf-location-field-master/acf-location.php');
include_once('add-ons/acf-repeater/acf-repeater.php');
include_once('add-ons/acf-options-page/acf-options-page.php');
include_once('add-ons/acf-taxonomy-field-master/taxonomy-field.php');
require ( get_template_directory() . '/add-ons/custom-widgets.php' );
require ( get_template_directory() . '/add-ons/custom-form.php' );
I am sending screens to assist in troubleshooting.
thank you!
From documentation:
All the API functions can be used with a taxonomy term, however, a second parameter is required to target the term ID. This is similar to passing through a post_id to target a specific post object.
The $post_id needed is a string containing the taxonomy name + the term ID in this format: β$TaxonomyName_$TermIDβ
Examples
Display a field
<p><?php the_field('field_name', 'thetaxonomyname_7'); ?></p>
By the way chaps thanks incredibly for the help, I was ready to gauge out my eyes. π
Mike J
No matter I fixed it. The problem was with “get_field” I replaced it with “the_field”
<?php
global $post;
// load all 'category' terms for the post
$terms = get_the_terms($post->ID, "product_brand");
// we will use the first term to load ACF data from
if( !empty($terms) )
{
$term = array_pop($terms);
$custom_field = the_field('contact_info', 'product_brand_' . $term->term_id);
// do something with $custom_field
}
?>
I’m afraid changing “category_” to “product_brand_” had no effect. It’s almost like that line has no way of getting the id of the current term. The address reads like this http://site/?product_brand=1st-call-mobility where “1st-call-mobility” is the term of the product_brand taxonomy.
“contact_info” is my ACF field slug, and “Brands” is a Custom taxonomy, whose slug is “product_brand”. Its a Woocommerce plugin, essentially for adding manufacturers to Woocommerce products.
I’ve tried printing this code before and it just wasn’t picking up the terms custom field in the print either.
Do I perhaps need to define “$custom_field” somewhere?
Is ‘contact_info’ a field for Category terms or Brand terms? Based on the field that works, I’m guessing the latter, in which case you’ll need to change the 2nd get_field argument:
$custom_field = get_field('contact_info', 'product_brand_' . $term->term_id);
I’m not exactly sure where this is supposed to go, so forgive the ignorance but if placed above or between any of the following nothing is returned:
<?php if(get_field('latest_news_or_reports', $post_object->ID) == "news"): ?>
<?php query_posts( array ('posts_per_page' => 5, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => '' ) ); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
If placed below I get:
Array
(
[36] => stdClass Object
(
[term_id] => 36
[name] => Events
[slug] => events
[term_group] => 0
[term_taxonomy_id] => 36
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 3
[object_id] => 2470
)
)
Which seems to indicate its grabbing the details from the most recent post (which is something in the Events category with an ID of 2470).
@wells5609 @elliot
Ok, so I managed to get this code to work to get a specific taxonomy ID (so I know the system is working)
<p><?php // the_field('contact_info', 'product_brand_14'); ?></p>
But trying to get the same terms of the current Term ID being displayed, doesn't work however. (In my “archive-brands” template.) Even with array_pop.
<?php
global $post;
// load all 'category' terms for the post
$terms = get_the_terms($post->ID, "product_brand");
// we will use the first term to load ACF data from
if( !empty($terms) )
{
$term = array_pop($terms);
$custom_field = get_field('contact_info', 'category_' . $term->term_id );
// do something with $custom_field
}
?>
Thanks guys
Seems like the $terms array is empty… You can test it with this code:
<?php
$terms = get_the_terms( $post->ID, 'category' );
echo '<pre>';
print_r( $terms );
echo '</pre>';
die;
?>
Can you confirm that 'category' is the correct taxonomy and $post->ID is the correct post ID?
Hi @udapud
Instead of using
‘
$term = $terms[0];
‘
Can you try
$term = array_pop($terms);
Does that fix the issue?
I had this exact same issue!
Not sure if this helps, but I was using the Taxonomy Field add-on by Brian Zoetewey previously, and disabled this when ACF gained this functionality. However the bug still remained. Today I removed the Taxonomy Field Add-On completely, and upgraded ACF to the latest version, and this functionality started working perfectly!! Such a winning feature.
One question though – it would be awesome if you could present the option to hide a CPT meta box from a edit screen? As it is, you can only hide default cateogies or tags. However, if we are using this feature, there is a good chance we are using a custom taxonomy that we want to hide – as we are replacing the default taxonomy meta box with our shiny new ACF meta box.
I know you can ‘hide’ this from the options panel up top, but a sneaky client might find this, and then have the ability to choose multiple tickboxes again π Be good to hide it permanetly if possible.
The first issue is not ACF related (its throwing an error before getting to the ACF function). If your taxonomy slug is ‘product_brand’, your get_the_terms() function should be
$terms = get_the_terms($post->ID, "product_brand");
In addition to checking if the term array is empty, you should check for a WP error like:
if ( $terms && ! $terms->is_wp_error() ) { ... }
Since WP_Error is an object, it can’t be used as an array like in $term[0].
The second code posted should work on a fresh install, provided you have a field called “field_name” (with a value) for taxonomy “Category” for the term with ID 7.
Your help documents say that to Display a field in my Archive template all I need do is include this code
<p><?php the_field('field_name', 'category_7'); ?></p>
With my relevant data. However I can’t seem to get this to work either. Is it possible you could perhaps include a code snippet example for every setup that works on fresh, unmodified installs? Whether it be for adding WYSIWYG’s or Calender or, like in my case, a simple text field. This would certainly help me a lot just now and I suspect a few more out there.
Great looking new forum btw π
Hi @Hube2
Thanks for the feedback. Totally agree and I will have a look at why this isn’t already added
Hi @Debatewise
Can I ask you to clarify what post types have what custom fields?
Does the Promo Box post type have a ‘taxonomy’ custom field? If so, what is the name of this field and what is the return type?
Next, where does the code come from that you have shown? Is that the code that is run for a Promo Box?
Most likely, all you need to do is use the get_field on the taxonomy custom field to find the term ID selected
Hi @JCIDEV
The $post_id parameter needs to be the $taxonomyName + $termID.
Your taxonomyName is actually ‘category’, not ‘alabama’ (alabama is your term name)
Modify your code to:
the_field('test_field', 'category_' . $term_id);
and that should work
Thanks for your quick reply. There’s probably something I’m not understanding about $term_id. I have a regular WP post category of “stories-by-state” which has subcategories of each state (alabama, alaska, etc.). I don’t think the fact that I’m retrieving subcategory info has any effect on the outcome.
Given your instruction, I would have something like the folowing:
the_field('test_field', 'alabama_' . $term_id);
or
the_field('test_field', 'alabama_16');
where 16 is the the number I get from the ur:l /wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=16&post_type=post
Neither worked and I”m not 100% sure how to pull $term_id.
You’ll need to set the second function parameter to ‘category-slug_2’ where ‘2’ is the term ID. So for example:
the_field('test_field', 'beer_' . $term_id);
would output the “test_field” value for the term with ID $term_id in the “beer” taxonomy.
Just to clarify, you are asking how do you load a custom field value from a taxonomy term when you know the taxonomy type and the term ID?
Please read this documentation article:
http://www.advancedcustomfields.com/resources/how-to/how-to-get-values-from-a-taxonomy-term/
Try replacing ‘category_’ with your custom taxonomy 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.