Wordpress-GD星级-在wp\U插入后设置等级

Wordpress-GD星级-在wp\U插入后设置等级,wordpress,rating,Wordpress,Rating,是否有可能在这之后对新帖子(使用管理员帐户)进行评分 $post_id = wp_insert_post( $my_post, $wp_error ); 发生这种情况后,您需要添加一个钩子。首先编写一个函数,然后执行以下操作: add_action('wp_insert_post', 'set_star_rating'); function set_star_rating() { global $post; $post = ... } 还有一个save_postshook,它

是否有可能在这之后对新帖子(使用管理员帐户)进行评分

$post_id = wp_insert_post( $my_post, $wp_error );

发生这种情况后,您需要添加一个钩子。首先编写一个函数,然后执行以下操作:

add_action('wp_insert_post', 'set_star_rating');
function set_star_rating() {
    global $post;
    $post = ...
}
还有一个
save_posts
hook,它在你保存帖子时出现,效果更好。

根据Wordpress支持论坛的建议,你应该这样做:

function set_rating($post_id, $vote) {  // $vote = 0..10
  $admin = get_user_by('login', 'admin');
  if ($admin !== false) {
    $ip = $_SERVER['SERVER_ADDR'];
    $ua = $_SERVER['HTTP_USER_AGENT'];
    gdsrBlgDB::save_vote($post_id, $admin->ID, $ip, $ua, $vote);
  }
  return $admin;
}

这对于基于GD Star Rating插件进行一些定制开发可能很有用。查找我的函数“保存类似帖子”,获取“帖子的类似计数”和“检查当前用户是否喜欢帖子”。这也适用于自定义帖子类型

/**
 * Function to save post like
 * @param type $post_id
 * @param type $user_id 
 */
function save_post_like($post_id, $user_id) {

    $ip = $_SERVER['SERVER_ADDR'];
    $ua = $_SERVER['HTTP_USER_AGENT'];

    if(has_user_liked_post($post_id) == 0)
        gdsrBlgDB::save_vote_thumb($post_id, $user_id, $ip, $ua, 1);

}

/**
 * Function to check if user like the post
 * @global type $wpdb
 * @global type $table_prefix
 * @param type $post_id
 * @return type 
 */
function has_user_liked_post($post_id) {

    global $wpdb, $table_prefix;

    $userdata = wp_get_current_user();
    $user_id = is_object($userdata) ? $userdata->ID : 0;

    $sql = "SELECT * FROM " . $table_prefix . "gdsr_votes_log WHERE vote_type = 'artthumb' AND id = " . $post_id . " AND user_id = " . $user_id;
    $results = $wpdb->get_row($sql, OBJECT);

    if (count($results))
        return 1;
    else
        return 0;
}

/**
 * Function to get total Likes of a Post
 * @global type $wpdb
 * @global type $table_prefix
 * @param type $post_id
 * @return type 
 */
function get_post_like_count($post_id) {

    global $wpdb, $table_prefix;

    $sql = "SELECT * FROM " . $table_prefix . "gdsr_data_article WHERE post_id = " . $post_id;
    $results = $wpdb->get_row($sql, OBJECT);

    if (count($results))
        return $results->user_recc_plus;
    else
        return 0;
}

我在这个文档中没有找到GD Star评级。你能给我举个例子吗?好的…你到底想做什么?我很乐意帮助你,但我找不到这个插件的任何文档或API。但我看到你自己在添加帖子,所以你甚至不需要设置钩子。你有ID,所以你只需要将评级发送到帖子ID。现在如果有文档,这个函数可能就在那里:PThanks Miha!这就是问题所在;)可捕获的致命错误:WP_User类的对象无法转换为第467行的…/…/db.php中的字符串:/@Peter我已编辑我的答案以解决此问题;为这个错误道歉。