如果您的问题是:
如果帖子的Posteta更改为预期值,我想发送电子邮件
您为什么要求:
如何在批量编辑时获取帖子id列表?
这是一个典型的x/y problem: 当你遇到问题时,问如何解决这个问题,而不是问如何应用你认为的解决方案。。。
在详细信息中,您想在更新元字段时执行操作吗?为什么不看看。。更新的那一刻?
每次更新帖子元时,WordPress都会调用挂钩\'updated_postmeta\'
就像这样
do_action("updated_{$type}_meta", $meta_id, $object_id, $meta_key, $meta_value);
哪里
$type
是
\'post\'
.
正如你所见,你有足够的信息去做任何你想做的事情。
假设您希望每次将meta“send\\u mail”设置为“ok”时都向帖子作者发送邮件:
add_action( \'updated_post_meta\', \'listen_to_meta_change\', 20, 4 );
function listen_to_meta_change( $mid, $pid, $key, $value ) {
if ( $key !== \'send_mail\' ) return; // if the key is not the right one do nothing
$value = maybe_unserialize( $value );
if ( $value !== \'on\' ) return; // if the value is not the right one do nothing
// if we\'re here, the post meta \'send_mail\' was set to \'on\' for a post, let\'s get it
$post = get_post( $pid );
if ( $post->post_type !== \'post\' ) return; // maybe check for a specific post type?
$recipient = new WP_User( $post->post_author ); // get the post author
if ( ! $recipient->exists() ) return; // check if is a valid user
static $sended = array();
if ( ! isset($sended[$recipient->ID]) ) $sended[$recipient->ID] = array();
if ( isset($sended[$recipient->ID][$pid]) ) {
// already sent email for this user & post & request: that\'s enough, let\'s exit
return;
}
$sended[$recipient->ID][] = $pid;
// ok, send the email, you can write the function, by yourself, isn\'t it?
// be sure to have control on how many emails you send to an user:
// too much emails slow down your site and also make you a spammer...
// probably you can take control that using an user meta...
send_email_to_user_when_meta_updated( $recipient );
}
请注意,此代码仅在元数据更新时运行,而在添加元数据时不运行。
要在添加meta时运行相同的代码,只需添加另一个操作:
add_action( \'added_post_meta\', \'listen_to_meta_change\', 20, 4 );
这两个钩子都以相同的方式工作,传递相同的参数,所以对这两个钩子使用相同的函数没有问题。