WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

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;


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.

share|improve this question
are you trying to use the wp_insert_post_data filter hook? – Chris Carson Aug 5 '11 at 15:11
Yeah, sorry. That's what I meant. I'm trying to obtain the post ID from within the hook. :) – Joey Morani Aug 5 '11 at 15:15
up vote 3 down vote accepted

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...
share|improve this answer
Ooh, thanks. Trying it out now. :) – Joey Morani Aug 5 '11 at 15:33
Okay, thanks. That got the ID. :) Having a slight problem getting "get_the_tags()" working though. Is this okay: paste.ideaslabs.com/show/mDP6UzCCMK – Joey Morani Aug 5 '11 at 16:06
Not sure what you are trying to do -- append the tags to post_content? The tags from the editor form are probably in $postarr. – Chris Carson Aug 5 '11 at 16:14
Ah. Yeah, you're right. I've done it now. Thanks again for your help! :) – Joey Morani Aug 5 '11 at 16:56
So there are 2 things that you want to do -- (1) modify the post data before saving and (2) email the contents of the post, including the tags and so on, after it's saved. Is that right? If so, you are still better off using save_post for the latter, because you can get the post in its saved state and not worry about synching up the unsaved with the saved. – Chris Carson Aug 5 '11 at 18:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.