Getting post id from wp_insert_post_data function?

I’m trying to use the function get_the_tags() from outside the ‘loop’.
I understand this can be achieved by using the post ID like get_the_tags($postID). Does anyone know how I can get the post ID from inside a wp_insert_post_data function?

I’ve tried using 'guid' which is suggested here, although I’ve had no luck. I’m also not sure that’s even the post ID. Any help with this will be appriciated. Thanks.

Read More

EDIT:
Here’s the code I’m working with:

function changePost($data, $postarr) {

  $postid = $postarr["ID"];
  $posttags = $postarr['tags_input']; // This doesn't work.

  $content = $data['post_content'];
  $subject = $data['post_title'];
  if($data['post_status'] == 'publish') {
    sendviaemail($content, $subject, $postid, $posttags);
  }

return $data;
}

add_filter('wp_insert_post_data','changePost','99',2);

As you can see, I want to send the post ID, post tags, content and the subject to another function called sendviaemail. Everything is okay, except I don’t know how to get the tags from the post.

Related posts

Leave a Reply

1 comment

  1. In the following, ’10’ is the priority that my_func gets called and ‘2’ is the number of arguments that my_func accepts. The latter is important, since the add_filter function defines the default as 1, but the wp_insert_post_data filter hook sends two arguments. If you don’t set this as 2 you won’t get the second argument.

    add_filter("wp_insert_post_data", "my_func", 10, 2);
    

    Now make your function…

    function my_func($data, $postarr){
        //at this point, if it's not a new post, $postarr["ID"] should be set
        //do your stuff...
        return $data;
    }
    

    EDIT— based on your added code above

    If you don’t need to modify the post’s $data before the post is saved then you’re using the wrong hook.

    Use the save_post action hook instead. This gets called after the post is saved and all the taxonomies are saved. So you don’t have to worry about whether new tags have been added. It sends two arguments to your function: the ID of the post and the post itself as an object.

    add_action("save_post", "my_save_post");
    function my_save_post($post_id, $post){
        if ("publish" != $post->post_status) return;
        $tags = get_the_tags($post_id); //an array of tag objects
        //call your email func etc...
     }