搜索热词

WordPress中文社区 > 问答 > WordPress发布文章publish_post钩子对自定义文章类型不起作用怎么办?

WordPress发布文章publish_post钩子对自定义文章类型不起作用怎么办?

已解决
//发布文章奖励声望
function publish_article_prestige( $post_ID ){
    global $wpdb; 
    $post_type = get_post_type( $post_ID );
    $user_id = get_post($post_ID)->post_author; 
    $old_pretige = get_user_meta( $user_id,"shengwang",true);
    if($post_type == 'course'){
       $apply_pretige = 10;
       $publish_type = "publish_course";
    } elseif($post_type == 'post'){
      $apply_pretige = 5;
      $post_cat = get_the_category($post_ID)[0]->cat_ID;
      if($post_cat == 2){
          $publish_type = "publish_question";
      } else {
          $publish_type = "publish_post";
      }
    }
    $date = date('Y-m-d H:i:s');
    $new_pretige = $old_pretige+$apply_pretige;
    $wpdb->insert('wp_prestige_log', array(
        'user_id' => $user_id,
        'post_id' => $post_ID,
        'old' => $old_pretige, 
        'apply' => $apply_pretige, 
        'new' => $new_pretige, 
        'type' => $publish_type, 
        'time' => $date, 
    ));
   update_user_meta($user_id,'shengwang',$new_pretige);
}
add_action('publish_post','publish_article_prestige');

 

发布于08月26日 被浏览 199 次
点赞 收藏 打赏 分享

    2 个回答

  1. WordPress日记

    WordPress 的 publish_post 钩子针对的是文章(帖子)类型,适用于 WordPress 站点的 post 类型的文章。如果想要发布页面时执行某些动作,对应钩子应该是 publish_page;如果你想发布某个自定义类型的文章,如课程类型course,那么对应钩子应该是 publish_course。针对你上面的代码,你可以如下修改:

    //发布文章或提问奖励声望
    function publish_article_prestige( $post_ID ){
        global $wpdb; 
        $user_id = get_post($post_ID)->post_author; 
        $old_pretige = get_user_meta( $user_id,"shengwang",true);
        $apply_pretige = 5;
        $post_cat = get_the_category($post_ID)[0]->cat_ID;
        if($post_cat == 2){
          $publish_type = "publish_question";
        } else {
          $publish_type = "publish_post";
        }
        $date = date('Y-m-d H:i:s');
        $new_pretige = $old_pretige+$apply_pretige;
        $wpdb->insert('wp_prestige_log', array(
            'user_id' => $user_id,
            'post_id' => $post_ID,
            'old' => $old_pretige, 
            'apply' => $apply_pretige, 
            'new' => $new_pretige, 
            'type' => $publish_type, 
            'time' => $date, 
        ));
       update_user_meta($user_id,'shengwang',$new_pretige);
    }
    add_action('publish_post','publish_article_prestige');
    
    //发布课程(自定义文章类型)奖励声望
    function publish_course_prestige( $post_ID ){
        global $wpdb; 
        $post_type = get_post_type( $post_ID );
        $user_id = get_post($post_ID)->post_author; 
        $old_pretige = get_user_meta( $user_id,"shengwang",true);
        $apply_pretige = 10;
        $publish_type = "publish_course";
        $date = date('Y-m-d H:i:s');
        $new_pretige = $old_pretige+$apply_pretige;
        $wpdb->insert('wp_prestige_log', array(
            'user_id' => $user_id,
            'post_id' => $post_ID,
            'old' => $old_pretige, 
            'apply' => $apply_pretige, 
            'new' => $new_pretige, 
            'type' => $publish_type, 
            'time' => $date, 
        ));
       update_user_meta($user_id,'shengwang',$new_pretige);
    }
    add_action('publish_course','publish_course_prestige');