5

I have a method connected to save_post action, WPML is being used as a translation plugin, I am trying to find a way that once a translation is added, and published is clicked and save_post method is triggered to know the ID of the original Post.

So far I have found that this can only be done when the post is already published and an Update is triggered. For this the method icl_object_id($translated_post_id, 'post', false, 'en' -> English is the language the original post will always be created in);

See comment MSG to see the above in context.

function my_project_updated_send_email( $post_id, $post, $update ){


  if ( 'publish' == get_post_status( $post_id ) && 'events' == get_post_type($post)) {

    if(ICL_LANGUAGE_CODE == 'en'){
     // Shortened - Everything works fine

    }elseif (ICL_LANGUAGE_CODE == 'it'){

      //Get English Language Post ID
      $id = icl_object_id($post_id,'post',false,'en');
      //MSG: $id returns empty on publish, but returns fine on update

      $event_id = get_field('event_id', $id);

      if($event_id == ""){
        // Shortened - Everything works fine
      }

    }

  }

}

add_action( 'save_post', 'my_project_updated_send_email', 10, 3);
Steve Cortis
  • 522
  • 1
  • 6
  • 25
  • I had a play and got the ID within the `save_post` hook every time I created a new post. But I don't have the WPML plugin so that may change things. Question - is the English post the parent to the other language posts? Could `wp_get_post_parent_id()` do the trick? – James Jones May 30 '17 at 06:41

1 Answers1

2

Issue is with the hook you are using i.e. 'save_post'

The "save_post" does not work when you publish a post. For that you may use 'publish_post' hook.

https://codex.wordpress.org/Plugin_API/Action_Reference/publish_post

You may also check if "pre_post_update" works in your case.

Muhammad Asadullah
  • 3,735
  • 1
  • 22
  • 38