Support

Account

Home Forums General Issues Update Taxonomy of posts selected by acf relation field

Solving

Update Taxonomy of posts selected by acf relation field

    • erfanmhd

    • September 10, 2019 at 6:51 am

    hi,

    i have created a taxonomy names: list (like tag not category) and assigned an acf relation field to it.
    i want to update all posts selected by relation field from taxonomy term page and assign the current taxonomy term to all selected post.

    Image

    Second Image

    i really appreciate it if you guys could give me a hint on how to do this.

    Thanks.

    • erfanmhd

    • September 11, 2019 at 4:41 pm

    any news guys ?

  • You would need to add an acf/save_post filter https://www.advancedcustomfields.com/resources/acf-save_post/

    In this filter you would test to see if you are saving a term in your taxonomy. If you are then you would get the field and call wp_set_post_terms() for each post in the relationship field https://codex.wordpress.org/Function_Reference/wp_set_post_terms

    • erfanmhd

    • September 12, 2019 at 8:52 am

    Thanks a bunch John,
    can you please help me with the code ?!

    function Taxonomy_List_Save( $post_id ) {
    
    	$PostList = get_field('downloads_listing', $post_id);
    	
    	if( $PostList ): 
    		foreach( $PostList as $List): // variable must be called $post (IMPORTANT) 
    			wp_set_post_terms( $post_id, $PostList->ID, 'list', true );
    		endforeach; 
    	endif;
        // Check if a specific value was sent.
        if( get_field('hero_image', $post_id) ) {
            // ...
        }
    }
    
    add_action('acf/save_post', 'Taxonomy_List_Save', 15);

    P.S: Pardon my bad coding skill… 🙁

    • erfanmhd

    • September 12, 2019 at 9:13 pm

    i’ve searched a bit and extend my code to this:

    function Taxonomy_List_Save( $post_id ) {
    
    	if is_tax( 'list' ) {
    		
    		$PostList = get_field('downloads_listing', $post_id);
    		
    		if( $PostList ) {
    			foreach( $PostList as $post) { 
    				setup_postdata($post);
    				wp_set_post_terms( $post_id, 'TaxonomyTermName', 'list', true );
    			} 
    			wp_reset_postdata();
    		}
    		
    	}
    
    }
    
    add_action('acf/save_post', 'Taxonomy_List_Save', 15);

    can you fix the code for me please? or give me hint on what’s missing ?

  • 
    function Taxonomy_List_Save( $post_id ) {
    
    	$PostList = get_field('downloads_listing', $post_id);
    	
    	if( $PostList ): 
    		foreach( $PostList as $List): // variable must be called $post (IMPORTANT) 
    			wp_set_post_terms( $post_id, $List->ID, 'list', true );
    		endforeach; 
    	endif;
        // Check if a specific value was sent.
        if( get_field('hero_image', $post_id) ) {
            // ...
        }
    }
    
    add_action('acf/save_post', 'Taxonomy_List_Save', 15);
    
    • erfanmhd

    • September 13, 2019 at 3:16 am

    Thanks @hube2 for your reply,
    but the code is not adding taxonomy term on post.

    i think we have to define two things
    1. limit this function to taxonomy “List Only”
    2. load the posts inside Taxonomy Term’s relation fields then add the current taxonomy term into “all posts selected with taxonomy term’s relation field”

    but the code i added is not doing anything actually … 🙁

  • Is list the name of your taxonomy? That’s about the only thing I think will cause an issue.

    This line should be limiting your the adding of terms to only things that have the values set

    
    if( $PostList ): 
    

    The post ID passed to your function for a term would be something like “term_100” where 100 will be the id of the term. You need to extract this.

    
    $term_id = intval(str_replace('term_', $post_id));
    

    and now that I’m looking at it the order of your arguments are also wrong.

    
    $posts = get_field('downloads_listing', $post_id);
    $term_id = intval(str_replace('term_', $post_id));
    if ($posts) {
      foreach ($posts as $post) {
        // make sure "list" is the taxonomy name and not the term name
        wp_set_post_terms($post->ID, $term_id, 'list', true);
      }
    }
    
    • erfanmhd

    • September 15, 2019 at 3:18 am

    i really appreciate your replies @hube2,
    but the code is not functional no matter what i try,

    i have a taxonomy(Tag Like) named: Download List
    i have a term inside “Download List” named: New Apps

    i have a post named : FirstApp

    now WHAT i need is a function:
    To add “NewApps” as a term of “Download List” Taxonomy, INSIDE “FirstApp” Post.

    When i open the edit page of “New Apps” (Taxonomy Term)
    and select “FirstApp” from acf relation field and update the taxonomy term.

    Believe me i’m searching any trying so hard to complete this function but still got no luck.

    i really really appreciate it if you can help me make this done.

  • In this line

    
    wp_set_post_terms($post->ID, $term_id, 'list', true);
    

    you need to change “list” to the slug of your taxonomy.

    • erfanmhd

    • September 16, 2019 at 2:47 am

    That line already is my Taxonomy Slug Bro,

    name: list
    label: Lists
    singular_label: List
    description: ""
    public: true
    publicly_queryable: true
    hierarchical: false
    show_ui: true
    show_in_menu: true
    show_in_nav_menus: true
    query_var: true
    query_var_slug: ""
    rewrite: true
    rewrite_slug: ""
    rewrite_withfront: true
    rewrite_hierarchical: false
    show_admin_column: true
    show_in_rest: true
    show_in_quick_edit: true
    rest_base: ""
    rest_controller_class: ""
    meta_box_cb: ""
    • erfanmhd

    • September 19, 2019 at 4:07 am

    any other idea ?!

  • I don’t see any reason why it shouldn’t be working, the only thing that I can think of is that you need to use the term slug instead of the ID when calling wp_set_post_terms(), but I don’t know why.

    • erfanmhd

    • September 20, 2019 at 4:30 am

    in general by using this code:

    
    function Taxonomy_List_Save( $post_id ) {
    		
    
    	$posts = get_field('downloads_listing', $post_id);
    	$term_id = intval(str_replace('term_', $post_id));
    	if ($posts) {
    	  foreach ($posts as $post) {
    	    // make sure "list" is the taxonomy name and not the term name
    	    wp_set_post_terms($post->ID, $term_id, 'list', true);
    	  }
    	}
    
    }
    
    add_action('acf/save_post', 'Taxonomy_List_Save', 15);

    in my functions.php i should be able to edit a taxonomy term (like tags) and select a post from “RelationField” of acf and by saving the tag term page the selected post should have that tag attached automatically ?
    cause this is what i’m looking for and neither by term id or slug i can’t make it work with the above code.

  • There is an error in my code I gave you

    This line should be causing an error in php

    
    $term_id = intval(str_replace('term_', $post_id));
    

    it should be

    
    $term_id = intval(str_replace('term_', '', $post_id));
    

    I also set up a test using posts and categories, this is working as expected

    
    add_filter('acf/save_post', 'category_list_save', 20, 1);
    function category_list_save($post_id) {
      $posts = get_field('posts', $post_id);
      $term_id = intval(str_replace('term_', '', $post_id));
      if ($posts) {
        foreach ($posts as $post) {
          // make sure "list" is the taxonomy name and not the term name
          wp_set_post_terms($post->ID, $term_id, 'category', true);
        }
      }
    }
    

    It it’s still not working for you make sure that your taxonomy and post type are attached. https://codex.wordpress.org/Function_Reference/register_taxonomy_for_object_type

    • erfanmhd

    • September 23, 2019 at 4:23 am

    Thanks for your reply,
    i think we have make a progress but there is some major problems.

    1. when i add a test taxonomy term and i select a post from ACF relationship field upon adding the taxonomy term a new taxonomy term with a number is being created beside the one i was adding and the selected post (inside ACF relation field) are being attached to that taxonomy term (number) not the one i want to create (please see the screenshot attached, “test” is what i was making and “173” is the one that is created automatically. )

    2. when i open a taxonomy term that has a post attached to itself ACF relation field is empty i want the relationship field to be live and i want to manage posts of a taxonomy term just by ACF relationship (instead of editing the posts one by one. )

    ScreenShot

    P.S: my taxonomy name is list, and my taxonomy and my post type are attached. i’ve double checked these.

Viewing 16 posts - 1 through 16 (of 16 total)

You must be logged in to reply to this topic.

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.