I try to use this function i have created when a post is published for the first time.
function a_new_post($post){
$post_id = $post->ID;
if ( !get_post_meta( $post_id, 'firstpublish', $single = true ) ) {
// ...run code once
update_post_meta( $post_id, 'firstpublish', true );
}
}
add_action( 'draft_to_published', 'a_new_post' );
I can’t see anything wrong with it, but when i try to create some sample posts i check the database and the field “firstpublished” has not been created.
Does anyone see anything wrong?
The correct action is
'draft_to_publish'
.To be sure you used the correct status try to get a list of all registered post statuses (including custom statuses) with:
On a vanilla installation you should get:
Note that
publish_post
is called each time you edit a published post.Note also
get_post_stati()
is one of these unpredictable names in WordPress: it is plain wrong. The plural of the noun status is statuses in English and statÅ«s in Latin. 😀You could also hook into
'transition_post_status'
, depending on your needs. You get the new and the old status as arguments, the third argument is the post object. It will catchfuture_to_publish
too, and also posts that were trashed once and republished now (trash_to_publish
).Example:
Post First save Time meta value set so easy to apply on first time.