0

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

2
  • Your question isn't 100%c clear to me. Is this right: You have posts on your site that have a favourite count as a meta value. You want to get the total favourite count of all posts belonging to a particular author? Commented Feb 23, 2018 at 5:30
  • Yes that right.
    – Chazlie
    Commented Feb 23, 2018 at 7:40

1 Answer 1

0
<?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();

It's also a good idea to check if simplefavorites_count meta key exists:

<?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',
        ),
    ),
);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.