Support

Account

Home Forums Backend Issues (wp-admin) Change taxonomy on Load_field problem!

Solving

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');
    
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Change taxonomy on Load_field problem!’ is closed to new replies.