Home › Forums › Backend Issues (wp-admin) › Change taxonomy on Load_field problem!
Hi,
I have a problem that i cant really solve on my own I guess… We have two post types (post & native). We wanna share a custom field group called “Story Group”. BUT when we are in native we wanna point to a different taxonomy then for post.
We have to different taxonomies.
– Post use Story
– Native use Campaign
I have made a filter for this. When we are one a native post_type use the campaign tax and when we are on a regular post use the Story tax.
This is how our filter looks like, only thing that happens in the select box is “Loading failed“.
function get_current_taxonomy( $field ) {
global $post;
if( 'native' === $post->post_type ) {
$field['taxonomy'] = 'campaign';
}
if( 'post' === $post->post_type ) {
$field['taxonomy'] = 'story';
}
return $field;
}
add_filter('acf/load_field/key=field_55411b948f60d', 'get_current_taxonomy');
But if i do it like this
function get_current_taxonomy( $field ) {
// OR THIS --> $field['taxonomy'] = 'campaign';
$field['taxonomy'] = 'story';
return $field;
}
add_filter('acf/load_field/key=field_55411b948f60d', 'get_current_taxonomy');
It works like a charm. So its something wrong when i pick the current post. I have also tried with get_current_screen() but thats no help either.
Any clues guys?
—- Versions —-
ACF v5.2.6
WP v4.2.2
Is this happening on for new post and existing posts, or just new posts.
I’m not sure that the global $post
will always contain what you expect it to. Try outputting what is there to test.
echo '<pre>'; print_r($post); echo '</pre>';
You may need to test for the post type in some other way for new posts.
~JH
@hube2 I have found a problem here I think. The select box populates the data through wp-admin/admin-ajax.php and when its doing that call it cant access $post or get_current_screen().
Is it possible in some way of knowing what screen in admin-ajax.php?
I just tried setting this up something similar to test and it’s working correctly with the code that you originally supplied. The only thing I’m not sure of is the field type at this point. Is this with a Taxonomy field or something else?
W0000t?!? :O
Its a taxonomy field and looks like this
array (
'key' => 'field_55411b948f60d',
'label' => 'Select Story',
'name' => 'story_post',
'prefix' => '',
'type' => 'taxonomy',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'taxonomy' => 'story',
'field_type' => 'select',
'allow_null' => 1,
'add_term' => 1,
'load_save_terms' => 1,
'return_format' => 'id',
'multiple' => 0,
),
Can you share your example?
Hey, sorry for the delayed response, for some reason my notifications on many discussions has got turned off.
I found the example code that I used. Different taxonomies but it is working.
function get_current_taxonomy( $field ) {
global $post;
if ('publication' === $post->post_type) {
$field['taxonomy'] = 'publication-type';
}
if ('post' === $post->post_type) {
$field['taxonomy'] = 'category';
}
return $field;
}
add_filter('acf/load_field/key=field_558aa7b9f6168', 'get_current_taxonomy');
You must be logged in to reply to this topic.
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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.