到目前为止,如果我在WordPress中将其作为页面运行,则此代码可以正常工作。当我发布带有特定标记的帖子时,我正在尝试发送我的用户电子邮件更新。用户可以选择是否要在其设置页面中接收更新。
每次发布帖子时,它都会检查帖子是否有特定的标签。只有一个标签将添加到帖子中然后检查标记是否与用户设置匹配(第一个用户设置),然后检查用户是否打开了更新。(第二次用户设置)
如果一切正常,则应发送电子邮件但如果代码在功能文件中,则不会发送电子邮件。我没有出错,所以我有点卡住了。
// check is new post is published
add_action( \'transition_post_status\', \'a_new_post\', 10, 3 );
function a_new_post( $new_status, $old_status, $post ) {
if ( \'publish\' !== $new_status or \'publish\' === $old_status ) {
return;
global $post; global $wp_query;
// get all the users meta
$blogusers = get_users();
// get users facility updates setting from settings page
$arg = array(
\'posts_per_page\' => \'1\'
);
// start the loop the get only the last post
$wp_query = new WP_Query($arg);
query_posts( \'posts_per_page=1 \');
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
// get the post tags
$posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) {
foreach ($blogusers as $user) {
// get the users settings and check if they match
$savedsetting = $user->facilityupdates;
$current_facility = $user->current_facility;
if($savedsetting === \'ON\' && $tag->name == $current_facility ) {
// get the users emails
$users_emails = $user->user_email . \',\';
// send email
$to = $users_emails;
$subject = "New update from PrisonPulse";
$message = "a caller who phone number is was sent to wanting to go to (second request) ";
$from = "[email protected]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
}}} }
endwhile; endif; // end the loop
}}
最合适的回答,由SO网友:Milo 整理而成
post的数据作为参数传递给连接到的函数transition_post_status
, 所以你不需要查询任何东西(同样,你的查询有点奇怪,使用WP_query和query_post,但这是另一回事)。下面是函数的精简版本,显示了重要的更改:
function a_new_post( $new_status, $old_status, $post ){
if ( \'publish\' !== $new_status or \'publish\' === $old_status )
return;
$posttags = get_the_terms( $post->ID, \'post_tag\' );
if( $posttags ) {
$blogusers = get_users();
foreach( $posttags as $tag ) {
// the rest of your code
}
}
}