Future post ID not showing

时间:2014-09-07 作者:Cody

我正在尝试在发布预定帖子时发送电子邮件通知。我试过很多不同的方法。我收到了电子邮件,但id(和其他随机详细信息)没有随邮件一起发布。这段代码中是否有我基本上缺少的东西?我尝试了内外两种方法以及其他一些方法。dap帖子是一个虚构的自定义帖子类型,我最初测试它,然后认为它可能是自定义帖子类型。

所以我用了一个普通的帖子,什么都没有。我还禁用了除包含以下代码的插件之外的所有插件。仍然没有任何效果。如果我立即发布,我可以看到详细信息,但不能从计划的时间开始。

add_action( \'init\', \'create_dap_post_types\' );

function create_dap_post_types() {
    register_post_type( \'dap\', 
        array(
            \'labels\' => array(
                \'name\' => __( \'Daps\' ),
                \'singular_name\' => __( \'Dap\' )
            ),
            \'public\' => true,
        )
    );
}



function status_changes( $new_status, $old_status, $post ) {
 global $post;
 global $wpdb;
 global $wp_query;

 if ($new_status == \'publish\') {
    //$post = get_post($id);
        $id = get_the_ID();
    $post = get_post($id);
    $post_title = $post->post_title;
      // $author = get_userdata($post->post_author);
     // the rest of your function here
     $link = get_permalink($post->ID);
    $date = mysql2date(\'M j Y\', $post->post_date);
    $category = get_the_category($post->ID);
    //$category = $category[0]->cat_name;

    $to = "//left blank to void out email";
    $subject = $post_title;
    $message = "<br><h1> schedulenotificte </h1></br> Let\'s begin: author " . //$author .
                " the link? " . $link;
    wp_mail( $to, $subjet, $message);
 }


}  
//add_action( \'transition_post_status\', \'status_changes\', 100, 3 );


add_action(\'publish_\'. $_POST[\'post_type\'], \'scheduledNotification\');
add_action(\'future_to_publish_dap\', \'scheduledNotification\');
add_action(\'new_to_publish_dap\', \'scheduledNotification\');
add_action(\'draft_to_publish_dap\', \'scheduledNotification\');
add_action(\'pending_to_publish_dap\', \'scheduledNotification\');
add_action(\'future_to_publish_dap\', \'scheduledNotification\');
add_action(\'auto-draft_to_publish_dap\', \'scheduledNotification\');
add_action(\'pending_to_future\', \'scheduledNotification\');
add_action(\'approved_to_future\', \'scheduledNotification\');

function scheduledNotification() {
  global $post;
  global $wp_query;
  $post = $wp_query->post;
  //$post = get_post($id);
  $my_postid = $post->ID;//This is page id or post id
  $content_post = get_post($my_postid);
  $content = $content_post->post_content;
  $content = apply_filters(\'the_content\', $content);
  $content = str_replace(\']]>\', \']]&gt;\', $content);

  $id = get_the_ID();
  $post = get_post($id);

  $link = get_permalink($post->ID);
  $date = mysql2date(\'M j Y\', $post->post_date);
  $category = get_the_category($post->ID);


  $to = "//left blank";
  $subject = $post->post_title;
  $message = "<br><h1> schedulenot </h1></br> <h2><br>Let\'s begin shall we?:<h2></br>author " . //$author .
            " the link? " . $link . 
            "<br> this is the content </br>" . $content .
            "<br> this is the idea</br>" . $post->ID;
  wp_mail( $to, $subject, $message);
}

function hereIsATest ($post) {
  global $post;
  global $wp_query;
  $post = $wp_query->post;

  $my_postid = $post->ID;//This is page id or post id
  $content_post = get_post($my_postid);
  $content = $content_post->post_content;
  $content = apply_filters(\'the_content\', $content);
  $content = str_replace(\']]>\', \']]&gt;\', $content);



  $subject = $post->title;
  $message = "<br> hereisatest </br> this is the id " . $post->ID . 
        "This is content " . $post->content .
        "<br> the title </br>" . $post->post_title .
        "what else there" . $subject . 
        "<br> id </br>" . get_the_ID() . 
        "<br> id_ </br>" . $post->ID .
        "<br> id </br>" . $post->content_post;
   wp_mail("//blank", $subject , $message);
}

add_action(\'publish_\'. $_POST[\'post_type\'], \'hereIsATest\');


add_action( \'transition_post_status\', \'inLoop\', 100, 3 );
add_action (\'wp_transition_post_status\', \'inLoop\');

function inLoop($new_status, $old_status, $post) {
  global $post;
  $thePostID = $post->ID;

  if ($new_status == \'publish\') {
    $to = "blank";
    $subject = $thePostID;
    $message = "<h1> This is insidethe loop</h1>" . $thePostID .
            "<br> the title ////" . get_the_title() .
            "<br> get the title </br>" . get_the_title() . 
            "<br> old status is: </br> " . $old_status . 
            "<br> new status is: </br> " . $new_status . 
            "<br> dump post: </br>" . $rpost;

    wp_mail($to, $subject, $message);
    }
}

add_action( \'transition_post_status\', \'outLoop\', 100, 3 );
add_action(\'wp_transition_post_status\', \'outLoop\');

function outLoop($new_status, $old_status, $post) {
  // Works in single posts outside of the loop
  global $wp_query;

  if ($new_status == \'publish\') {
    $thePostID = $wp_query->post->ID;
    //$rpost = print_r($post, true);
    $to = "email";
    $subject = $thePostID;
    $message = "<h1> This is outside the loop</h1>" . $thePostID . 
            "<br> get the title </br>" . get_the_title() . 
            "<br> old status is: </br> " . $old_status . 
            "<br> new status is: </br> " . $new_status . 
            "<br> dump post: </br>";

    wp_mail($to, $subject, $message);
   }

}

1 个回复
最合适的回答,由SO网友:cybmeta 整理而成

问题是您正在使用get_the_ID() 在圈外。此外,您将获得global $post 覆盖$post 对象传递给函数,然后使用get_post() 作用因为您已经$post 对象的状态已更改,则不需要get\\u the\\u ID、get\\u post或global$post。只需使用$post 您已经拥有的对象。

add_action( \'transition_post_status\', \'status_changes\', 100, 3 );
function status_changes( $new_status, $old_status, $post ) {

    if ($new_status == \'publish\') {

        $post_title = $post->post_title;
        $author = get_userdata($post->post_author);
        $link = get_permalink($post->ID);
        $date = mysql2date(\'M j Y\', $post->post_date);
        $category = get_the_category($post->ID);
        $category = $category[0]->cat_name;

        $to = "//left blank to void out email";
        $subject = $post_title;
        $message = "<br><h1> schedulenotificte </h1></br> Let\'s begin: author " . //$author .
            " the link? " . $link;
        wp_mail( $to, $subjet, $message);
     }


}

结束

相关推荐

如果我将POSTS表中代表文章修订的所有行都删除到WordPress数据库中,会发生什么?

我对posts WordPress数据库的表。我看到,当我在这个表中创建一个新帖子时,会自动创建2个新行。32 1 2014-08-16 15:07:22 2014-08-16 15:07:22 TEST REVISION TEST REVISION inherit open open 31-revision-v1 2014-08-16 15:07:22 2014-08-16 15:07:22 31 http://localhost/w