发布的帖子正在工作后发送通知,但在上次WP更新后停止

时间:2019-03-23 作者:Adham Mohamed

我只是想知道为什么这个片段不起作用了!它在上次WordPress更新之前就已经运行了!

我正在使用DW Notifications 插件并在其他功能中正常工作。

有任何解释,谢谢。

function to_followers_only_notify( $new_status, $old_status, $post ) {

    global $post;

    if ( \'publish\' !== $new_status or \'publish\' === $old_status )
        return;

        $post = get_post($post->ID);
        $author = $post->post_author;
        $author_name = get_the_author_meta( \'display_name\', $author );

        $followers = get_user_meta($author, \'_pwuf_followers\', true);
        $followers = array_unique($followers);

        $title = \'<strong>\'.$author_name.\'</strong> \' . __(\'Published new post\', \'dw-notifications\'). \' <strong>\'.$post->post_title.\'</strong>\';
        $link =  get_permalink($post->ID);
        if(has_post_thumbnail($post->ID)){
            $image = get_the_post_thumbnail_url($post->ID, \'hitmag-list\') ;
        }

        $notif = array(\'title\'=>$title, \'link\' => $link, \'image\' => $image);

        if ( !empty( $followers ) && is_array( $followers ) ) {

            $follower_ids = array();
            foreach( $followers as $follower ) {
                if ( is_numeric( $follower ) ) {

                    if(!$post->post_author || $post->post_author == $follower) continue;


                    $follower_ids[] = $follower;

                }
            }
            dwnotif_add_notification($follower_ids, $notif);
        }
}
add_action( \'transition_post_status\', \'to_followers_only_notify\', 10, 3 );

Update 1

当我使用publish_post 再次开始行动!但我需要避免每次更新帖子:(,请提供任何建议,谢谢。

function to_followers_only_notify( $ID, $post ) {

    global $post;

        $post = get_post($post->ID);
        $author = $post->post_author;
        $author_name = get_the_author_meta( \'display_name\', $author );

        $followers = get_user_meta($author, \'_pwuf_followers\', true);
        $followers = array_unique($followers);

        $title = \'<strong>\'.$author_name.\'</strong> \' . __(\'Published new post\', \'dw-notifications\'). \' <strong>\'.$post->post_title.\'</strong>\';
        $link =  get_permalink($post->ID);
        if(has_post_thumbnail($post->ID)){
            $image = get_the_post_thumbnail_url($post->ID, \'hitmag-list\') ;
        }

        $notif = array(\'title\'=>$title, \'link\' => $link, \'image\' => $image);

        if ( !empty( $followers ) && is_array( $followers ) ) {

            $follower_ids = array();
            foreach( $followers as $follower ) {
                if ( is_numeric( $follower ) ) {

                    if(!$post->post_author || $post->post_author == $follower) continue;

                    $follower_ids[] = $follower;

                }
            }
            dwnotif_add_notification($follower_ids, $notif);
        }
}
add_action( \'publish_post\', \'to_followers_only_notify\', 10, 2 );

1 个回复
SO网友:Antti Koskinen

关于Update 1. 也许您可以比较发布日期和修改日期,并仅在它们匹配时(即文章首次发布时)运行代码。沿着这些路线,

function to_followers_only_notify( $ID, $post ) {

  $posted = strtotime( $post->post_date );
  $modified = strtotime( $post->post_modified );

  if ( $posted !== $modified ) {
    return;
  }

  // rest of the code
}