自定义帖子类型的wp_is_post_revision()始终返回false

时间:2018-04-14 04:44:14

标签: php wordpress

我正在使用wp_mail()在发布新的自定义帖子类型sfwd-lessons时发送电子邮件。

我只想发送新帖子,而不是修改。

我认为这只会发送新帖子:

if ( !wp_is_post_revision( $post_id ) ) { 

...但在现有帖子更新时,它仍在发送电子邮件。

完整代码:

// SEND EMAIL ONCE LESSON IS CREATED
function notify_subscriber_new_lesson($post_id) {

    //verify post is not a revision 
if ( !wp_is_post_revision( $post_id ) ) { 

        //gets subscribers to send email to
        // WP_User_Query arguments
        $args = array (
            'role'           => 'Subscriber',
        );


        // The User Query
        $user_query = new WP_User_Query( $args );

        // get email addresses from user objects
         $email_addresses = array();
        foreach ( $user_query->results as $user ) {
            $email_addresses[] = $user->user_email;
        }

        // build message
        $post_title = get_the_title( $post_id ); 
        $post_url = get_permalink( $post_id ); 
        $subject = 'Now available: ' . $post_title; 
        $url = "<a href='". $post_url. "'>" .$post_title. "</a>"; 

            ob_start(); ?>

    <html>
        <head>
            <title>New Download at Website</title>
        </head>
        <body>
            <p>
                Hi <?php echo $user->user_firstname?>,
            </p>
            <p>
                The latest easy to download Video and Podcast is available!
            </p>
            <p>
                <?php echo $url ?>
            </p>
            <p>
                Be sure to log in, download, and gain those Points!
            </p>
            <p>
                Regards,<br />
                Site Owner,<br />
                Company.
            </p>
        </body>
    </html>

    <?php
    $message = ob_get_contents();

    ob_end_clean();

        //send email to all emails
        wp_mail($email_addresses, $subject, $message );
}
}
add_action( 'publish_sfwd-lessons', 'notify_subscriber_new_lesson' );

1 个答案:

答案 0 :(得分:1)

您应该使用函数 wp_get_post_revisions(),此时您正在检查父帖是否是修订版,这就是它始终返回false的原因。如果父项包含修订,此函数将返回子项目数组。

$revisions = wp_get_post_revisions( $post->ID );

if( count( $revisions ) === 0 ) {
    //Send an email
}