3

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.

  • 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 Jun 3 '15 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 Jun 3 '15 at 17:13
7

Read the codex, that's where i finally found the answer :

https://codex.wordpress.org/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 );
3

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.

0

s_ha_dum - you were correct this is a proper hook - my bad.

I checked the DB table on the other blog and the post does actually get copied - it just does not show up in the admin list for events on the other site. So I'll have to figure out why the copied post in not displaying in the list.

0
  • {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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.