我正在尝试获取特定自定义帖子类型的meta_key数量的排名。 但是我不太擅长SQL,因此不适合使用$ wpdb。
例如,如果我写这样的话,
<?php
$tarms = array( ‘sweet, sour’ );
echo get_count_ranking( $tarms );
?>
然后,我想按“计数的键”按“甜食的期限”和“酸的期限”的顺序显示“ fluits的自定义帖子”的comment(“ custom的回复”)排名。>
这是我的代码:
function get_count_ranking( $tarms ){
global $wpdb;
$counts = $wpdb->get_results( $wpdb->prepare( "
SELECT p.post_author AS user_id, sum(m.meta_value) AS SumUser
FROM $wpdb->posts AS p, $wpdb->postmeta AS m
WHERE p.ID = m.post_ID
AND p.ID IN (
SELECT tr.object_id
FROM $wpdb->term_relationships AS tr, $wpdb->posts AS p, $wpdb->term_taxonomy AS tt
WHERE p.post_type = 'reply'
AND tt.term_id = %s
AND p.id = tr.object_id
AND tr.term_taxonomy_id = tt.term_taxonomy_id
)
AND p.post_status = 'publish'
AND m.meta_key = 'count'
GROUP BY p.post_author
ORDER BY m.meta_value DESC LIMIT 10
", $tarms ) );
$result = '';
foreach ( $counts as $count ) {
$result .= '<li><img>'.get_avatar($count->user_id, 30).'<span></span></li>';
}
return $result;
}
对不起,我的英语太差了。 因此,我附上这张图片供您参考。 谢谢。
-
更新的代码:
function get_count_ranking( $tarms ){
$customPostArg = array(
'posts_per_page' => 5,
'post_type' => 'fluits',
'tax_query' => array(
array(
'taxonomy' => 'taste-tag',
'field' => 'slug',
'terms' => $tarms
)
)
);
$array_with_post_ids = get_posts($customPostArg);
$argsp = array(
'post__in' => $array_with_post_ids
);
$commentsp = get_comments( $argsp );
$needed_data_array = array();
foreach ($comments as $key => $comment) {
$ranking = get_comment_meta($comment->ID, 'count', $return_single_value = true);
$author_id = $comment->user_id;
// make sure we have an author id
if($author_id) {
$needed_data_array[$author_id][] = $ranking;
}
}
}
$tarms = array( ‘sweet, sour’ );
echo get_count_ranking( $tarms );
答案 0 :(得分:0)
我不会为此使用$wpdb
。
另外,看起来您将排名保存到POST元字段中,我将使用注释元字段。
请注意,我不会帮助您编写所有内容,但这是 一些指针,我将如何做到这一点。
现在,帖子评论和排名已保存。
收集数据
WP有一个get_comments()
函数,该函数接受许多参数。可悲的是,我错过了一个论点,希望从具有特定分类法的帖子中获取评论。因此,我们必须先收集所有帖子:
get_posts()
。get_comments()
获取所有与帖子相关的评论。示例:
$args = array(
'post__in' => $array_with_post_ids,
);
$comments = get_comments( $args );
现在,您已经拥有创建概述所需的所有注释,我将遍历(迭代)它们并使用author_names及其排名构建一个数组。
示例:
$needed_data_array = array();
foreach ($comments as $key => $comment) {
$ranking = get_comment_meta($comment->ID, 'ranking_meta_key', $return_single_value = true);
$author_id = $comment->user_id;
// make sure we have an author id
if($author_id) {
$needed_data_array[$author_id][] = $ranking;
}
}
//
// Now the $needed_data_array holds all authors
// and their post rankings, you can count them
// to get ranking totals for each comment-author.
//