I am trying to write a plugin that copies a custom post type "event" from one blog to another. The code for copying I already have working but I cannot seem to get this "add_action" hook to work when an event is published.
add_action('publish_event', 'copy_event_to_mini_site' );
function copy_event_to_mini_site() {
code in here to copy relevant data from one blog to the other
}
I also tried: add_action('publish_post', 'copy_event_to_mini_site' );
That did not work either.
I am trying to write a plugin that copies a custom post type "event" from one blog to another. The code for copying I already have working but I cannot seem to get this "add_action" hook to work when an event is published.
add_action('publish_event', 'copy_event_to_mini_site' );
function copy_event_to_mini_site() {
code in here to copy relevant data from one blog to the other
}
I also tried: add_action('publish_post', 'copy_event_to_mini_site' );
That did not work either.
Share Improve this question edited Jun 3, 2015 at 16:54 Pieter Goosen 55.4k23 gold badges115 silver badges210 bronze badges asked Jun 3, 2015 at 16:51 ChrisChris 551 gold badge1 silver badge6 bronze badges 2- That is a proper trasnsition status hook. Exactly what is or isn't happening and what exactly do you expect (want) to happen? – s_ha_dum Commented Jun 3, 2015 at 17:10
- I want to call the code to copy the post being "published" on one network site to another to be called when the "publish" button is clicked in the "event" post edit screen in admin. At the moment it looks like its not being executed. – Chris Commented Jun 3, 2015 at 17:13
3 Answers
Reset to default 9Read the codex, that's where i finally found the answer :
https://codex.wordpress/Plugin_API/Action_Reference/publish_post
in the end :
Custom Post Types
To trigger this action for a custom post type, use publish_{$custom_post_type}. e.g. if your post type is 'book' use:
add_action( 'publish_book', 'post_published_notification', 10, 2 );
transition_post_status
helps you to perform for all post types and change from one status to other,say for example, pending to publish. or new publishing post.
Here is official WordPress Codec Page .
function on_all_status_transitions( $new_status, $old_status, $post )
{
if ( $new_status != $old_status ) {
// A function to perform actions any time any post changes status.
}
if ( $new_status != 'publish' ) {
// A function to perform action when new post published.
}
}
add_action( 'transition_post_status', 'on_all_status_transitions', 10, 3 );
I hope this will help universally for all custom post types and default post types.
- {old_status}to{new_status}
- {status}_{post_type}
The available post statuses are:
- new – When there’s no previous status (this means these hooks are always run whenever "save_post" runs.
- publish – A published post or page.
- pending – A post pending review.
- draft – A post in draft status.
- auto-draft – A newly created post with no content.
- future – A post scheduled to publish in the future.
- private – Not visible to users who are not logged in.
- inherit – A revision or attachment (see get_children()).
- trash – Post is in the trash (added with Version 2.9).
You can create events per post status transitions as you need.