正如您所描述的,您希望在帖子被丢弃、编辑或取消刷新时同步数据。您正在尝试save_post
操作,但它仅在编辑后屏幕上触发(根据codex,此操作在导入、发布/页面编辑表单、xmlrpc或通过电子邮件发布时触发),因此您认为还需要挂接批量操作和快速编辑,但您错了。通过批量操作或快速编辑编辑帖子时,不会触发特定的保存操作(您发布的关于添加自定义bulck操作的链接是为了添加自定义批量操作,而不是为了在预定义的批量操作上执行任务)。
在我看来,没有什么比同步数据更适合后期状态转换了。
您尝试了后状态转换,但逻辑错误。例如,当一篇文章从“发布”转换为“未发布”时,您可以运行“删除同步”功能,但这种情况并不意味着一篇文章被删除:一篇未发布的文章可能会被丢弃,一篇草稿,一篇未来的文章,等等。
让我们看一些例子:
add_action( \'transition_post_status\', \'cp_sync\', 10, 3 );
function cp_sync( $new_status, $old_status, $post ) {
// Check $old_status also if you need specific actions
// when the post transits from a specific status
if ( $new_status == \'draft\' ) {
// The post is now draft, no matter what status it had previously
// sync draft posts here
}
if ( $new_status == \'publish\' ) {
// The post is now published, no matter what status it had previously
// sync published posts here
}
if ( $new_status == \'trashed\' ) {
// The post is now in the trash, no matter what status it had previously
// sync trashed posts here
}
// Cotinue checking more post statues if you need (future, private, etc)
// For a complete list, see https://codex.wordpress.org/Post_Status_Transitions
}
删除的帖子没有状态转换,您必须使用
actions described in the docs for delete_post
action; 我会使用
before_delete_post
如果需要捕获所有post数据:
add_action( \'before_delete_post\', \'my_func\' );
function my_func( $postid ){
// Post has been deleted
pluginname_sync::pluginname_delete( $postid );
}
编辑:仅同步已发布的帖子,从外部数据库中删除其余帖子:
add_action( \'transition_post_status\', \'cp_sync\', 10, 3 );
function cp_sync( $new_status, $old_status, $post ) {
// This will cover the transition from any status to published,
// including published to published.
if ( $new_status == \'publish\' ) {
pluginname_sync::pluginname_syncpost($post->ID);
} else {
pluginname_sync::pluginname_delete($post->ID);
}
}
add_action( \'before_delete_post\', \'my_func\' );
function my_func( $postid ){
pluginname_sync::pluginname_delete( $postid );
}