I am using the plugin https://favoriteposts.com.
To be a little clearer I have posts on my site that have a favourite count as a meta value. I want to get the total favourite count of all posts belonging to a particular author which the displays on each authors author.php
I have about 300 authors on my site and they would like to see a final count all their posts that have been Favorited on their profile (which is the author.php) So for example Post 1 has been Favorited 4 times Post 2 has been Favorited 3 times Post 3 has been Favorited 6 times Total Favorites = 13
I have managed to get it to bring out a list of posts with a favorite value (When a post has never been Favorited the value is empty, when it gets Favorited the value adds 1, when it gets unFavorited it goes to 0.) So I can get it to output any posts with a field value whether its 0,1 or 44 using this code
<?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();
}?>
Ive tried this also Which did not work at all
<?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>
I also tried this which also did not work
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);
Can anyone advise please
Thanks