在save_post中将ID传递给已翻译的帖子

Sit*_*ock 5 wordpress wordpress-theming custom-post-type wpml advanced-custom-fields

我有一个连接到save_post动作的方法,WPML被用作翻译插件,我试图找到一种方法,一旦添加了翻译,点击发布并触发save_post方法以了解原始帖子的ID.

到目前为止,我发现这只能在帖子已经发布并触发更新时才能完成.为此,方法icl_object_id($ translated_post_id,'post',false,'en' - >英语是始终在其中创建原始帖子的语言);

请参阅评论MSG以在上下文中查看上述内容.

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);
Run Code Online (Sandbox Code Playgroud)

Moh*_*sin 2

问题在于您使用的挂钩,即“save_post”

当您发布帖子时, “ save_post”不起作用。为此,您可以使用 ' publish_post' 钩子。

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

您还可以检查“ pre_post_update ”是否适合您的情况。