Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My plugin uses publish_post hook to get the post data and do some processing:

add_action('publish_post', 'publish_post');

function publish_post($post_id) {
      global $post, $blog_id;
      $author = get_userdata ($post->post_author);
      $title = $post->post_title;
      //some processing here...
}

I notice that for a new post, $title is always blank, while all other fields are available. If I publish the post again (update), then the title becomes available.

Edit

I printed out the $post variable and notice that that the post_status is draft:

[post_title] =>
[post_excerpt] =>
[post_status] => draft

So it seems the $post variable contains what was loaded from the database, but not what is currently on the screen. How should I get the $post that reflects what is currently on the screen? Is there a hook that fires after a post is saved to the database?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

This hook is called like this:

do_action("{$new_status}_{$post->post_type}", $post->ID, $post);

My guess is that you should be using data passed as second $post argument, rather than global.

So:

add_action('publish_post', 'publish_post', 10, 2 );

function publish_post($post_id, $post) {

    // stuff
share|improve this answer

Your Answer

 
discard

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.