Home Forums Backend Issues (wp-admin) get_field() empty on save_post when bulk editing


get_field() empty on save_post when bulk editing

  • Hi guys

    I’ve written a small function that is trigged when saving a a cpt ‘myCPT’

    • I first clears the post term of a custom taxnomomy ‘custom-tax’
    • then it get the value of an acf relationship field ‘myRelationshipField’
    • loop throug the results to
      • get the value of a meta field ‘shadow_term_id’ of the related post ; it actually get the id of a term of my custom-tax
      • and use this value to add this term to the original post

    it works nicely in the post editor,

    but if Iedit my post from the post list, with the bulk editor, changing post statut for instance, then it delete the terms, and do not re-add them. The relationship is kept, and I can “restore” the terms by saving the post from the editor again.

    Do you guys have any clues what’s going on, and how to fix that ?

    Thank’s in advance

    add_filter( 'save_post', 'change_post_data_before_save', 10, 2 );
    //add_filter( 'acf/save_post', 'change_post_data_before_save' ); // nothing happens
    function change_post_data_before_save( $post_id, $post ) {	
        if ( 'myCPT' == $post->post_type ) {
          wp_set_object_terms( $post_id , null, 'custom-tax', false );    
          $related_post = get_field('myRelationshipField');
          if($related_post) {
             foreach( $related_post as $related) {
         	      $related_id = $related-> ID;
              	$related_shadow_term_id = (int) get_post_meta( $related_id, 'shadow_term_id', true );
    	     		wp_set_object_terms( $post_id , $related_shadow_term_id, 'custom-tax', true );
  • In this context you must supply the post ID to get the field from

    $related_post = get_field('myRelationshipField', $post_id);
  • Hi

    Spot-on !

    This now seems obvious, thanks

    In the meantime, I had found an other way to restrict the execution of my fuction on the post edit page by nesting it into this

    global $pagenow;
    if (( $pagenow == 'post.php' )) { 
      //do your stuff
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.