在此之后是否可以对新帖子(使用管理员帐户)进行评分?
$post_id = wp_insert_post( $my_post, $wp_error );
根据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;
}
发生这种情况后,您需要添加一个钩子。先写一个函数,然后这个:
add_action('wp_insert_post', 'set_star_rating');
function set_star_rating() {
global $post;
$post = ...
}
save_posts
在您保存帖子时还会发生钩子,并且有更好的文档记录。
这对于基于 GD Star Rating Plugin 进行一些自定义开发可能很有用。找到我的功能来“保存喜欢的帖子”,获取“帖子的点赞数”和“检查当前用户是否喜欢帖子”。这也适用于自定义帖子类型。
/**
* 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;
}