Good morning,
I need a hook or something to call to separate new posts from updated ones.
I want to execute some code only for new posts… something like that:
add_action('publish_post', myfunction);
function myfunction( $post_id ){
if <new post> {
/* do something */
}
else {
/* do nothing */
}
}
I’m using WP v3.5
Any suggestion?
First, you want to use the draft_to_publish hook:
However, this can fire multiple times if a post is published, unpublished, and then published again. To ensure that your action only fires one time, set a custom meta element for the post at the end of your function. At the start of your function, check if you meta element is set and skip the action if it is.
— Added —
Here is a sample on how the repeat action protection would work (untested code – so might need a little work):