获取作者最喜欢的帖子总数

时间:2018-02-23 作者:Chazlie

我正在使用插件https://favoriteposts.com.

更清楚一点,我在我的网站上有一些帖子,其中有一个最受欢迎的元值。我想获得属于某个作者的所有帖子的最受欢迎总数,这些帖子会显示在每个作者的页面上。php

我的网站上有大约300位作者,他们希望看到最后一次统计他们在个人资料(即author.php)上受到青睐的所有帖子,因此,例如,帖子1受到青睐4次帖子2受到青睐3次帖子3受到青睐6次总收藏夹=13

我已经设法让它显示一个带有收藏夹值的帖子列表(当一篇帖子从未受到青睐时,该值为空,当它受到青睐时,该值加1,当它不受欢迎时,该值变为0)所以我可以让它用这个代码输出任何带有字段值的帖子,无论是0、1还是44

    <?php

$args22 = array(
    \'post_type\' => array (\'menus\', \'recipes\' ), 
    \'author\' => get_queried_object_id(),
    \'meta_query\' => array( 
        \'meta_key\' => \'simplefavorites_count\',
        \'value\'   => \'0\',
        \'compare\' => \'>=\' 
        )

);


$my_query = new WP_Query( $args22 );?>

Found posts: <?php echo $my_query->found_posts;?>

<?php if ( $my_query->have_posts() ):?>
    <ul>
        <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
            <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> Has been added to <?php echo get_post_meta( get_the_ID(), \'simplefavorites_count\', true );?> saves</li>

        <?php endwhile; ?>
    </ul>
<?php endif; ?>

<?php wp_reset_postdata();


}?>  
我也试过了,但根本不起作用

   <?php


global $wpdb;

        $query = "SELECT SUM(meta_value) AS favorites_count FROM {$wpdb->prefix}postmeta WHERE meta_key = \'simplefavorites_count\' AND post_author = \'1\'";
        $count = $wpdb->get_var( $query );


?>
<h2>Post Count: <?php echo $count; ?></h2>
我也试过了,但也没用

    global $wpdb;
global $post;

$sql = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE
    $wpdb->posts.ID = $wpdb->postmeta.post_id
    AND $wpdb->posts.post_status = \'publish\'
    AND   $wpdb->postmeta.meta_key = \'simplefavorites_count\'
    AND $wpdb->posts.post_author = $curauth->ID
    )
GROUP BY $wpdb->posts.ID
ORDER BY $wpdb->posts.post_date DESC
";



// Results can be parsed as OBJECT, OBJECT_K, ARRAY_A, ARRAY_N
$result = $wpdb->get_results($sql, OBJECT);

// See your results printed out (optional)
print_r($result);
有人能给我建议吗

谢谢

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

<?php
$args22 = array(
    \'post_type\'  => array (\'menus\', \'recipes\' ),
    \'author\'     => get_queried_object_id(),
    \'meta_query\' => array(
        \'meta_key\' => \'simplefavorites_count\',
        \'value\'    => \'0\',
        \'compare\'  => \'>=\',
    ),
);

$my_query = new WP_Query( $args22 );

echo \'Found posts: \' . $my_query->found_posts . \'<br />\';

// declare variable and set total votes to 0
$total_fav_count = 0;

if ( $my_query->have_posts() ) {
    while ( $my_query->have_posts() ) {}
        $my_query->the_post();

        // add custom field value to total votes
        $total_fav_count += get_post_meta( get_the_ID(), \'simplefavorites_count\', true );
    }
}

//
echo \'Total user favorite votes: \' . $total_fav_count . \'<br />\';

wp_reset_postdata();
检查一下simplefavorites_count 元密钥存在:

<?php
$args22 = array(
    \'post_type\' => array (\'menus\', \'recipes\' ), 
    \'author\' => get_queried_object_id(),
    \'meta_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'meta_key\' => \'simplefavorites_count\',
            \'value\'   => \'0\',
            \'compare\' => \'>=\',
        ),
        array(
            \'meta_key\' => \'simplefavorites_count\',
            \'compare\' => \'EXISTS\',
        ),
    ),
);

结束