Igor Yavych
Igor Yavych

Reputation: 4244

Action only when new post is published

I'm writing plugin for myself and I need some function to fire off when new post is published. So far I found http://codex.wordpress.org/Post_Status_Transitions which says that there is action for each pair of statuses. I want to know which action function should fire off. I tried new_to_publish (didn't fire off) and draft_to_publish (worked as intended) and auto-draft_to_publish (worked half intended. was able to get ID from global post variable (to get link via get_permalink()) but title (in post variable) was set to Auto Draft instead of actual title. So question is, what action should I actually use? I assume it should be both auto-draft_to_publish and draft_to_publish but in that was I want to know how to get actual title instead of Auto Draft

Upvotes: 0

Views: 1710

Answers (2)

Igor Yavych
Igor Yavych

Reputation: 4244

I decided to use auto-draft_to_publish and draft_to_publish. First happens when totally new post is published, second happens when draft is published. I didn't notice at first that both pass $post variable (I used global $post first time which caused "Auto Draft" title). With this my problem solved.

Upvotes: 0

Mark
Mark

Reputation: 3035

Use the publish_post and publish_future_post actions.

As you would expect, these fire when a post is published and when it is triggered to publish if set to a future date.

EDIT:

To make sure that the post is being published check the modified date with the post date.

function publishing_post( $post_id ) {
  $post = get_post( $post_id );

    if( $post->post_modified <> $post->post_date ) return;

}

Upvotes: 1

Related Questions