Running a voting system on my site that lets users vote on posts with the standard up/down type thing.
I'm trying to run a query on author pages to get the total number of 'votes' that author has attributed to them over all the posts they've made.
Small problem is that when the author has zero votes it displays the total number of votes from the meta_key value for every author on the site.
Code:-
<?php
function author_rating_total() {
$user_id = get_the_author_meta( 'ID' );
$query = array (
'author' => $user_id,
'suppress_filters' => 'true', //lets skip some unnessecery sql queries
'posts_per_page' => -1
);
$queryObject = new WP_Query($query); while($queryObject->have_posts()) : $queryObject->the_post();
$post_ratings_data = get_post_custom(get_the_id());
$post_ratings_score = intval($post_ratings_data['votecount'][0]);
$ratings_array[] = $post_ratings_score;
endwhile;
$ratings_sum = array_sum($ratings_array);
if ($ratings_sum > 0) {
$ratings_sum = '' . $ratings_sum;
}
echo $ratings_sum;
wp_reset_query();
}
?>
<?php
echo author_rating_total();
?>
How do I return the value 0 instead of the total number of that meta_key?
Related
On my site, I intend to display the total number of posts and comments on the site, as well as the total number of purchases made from my site.
The codes I wrote are as follows:
//copy to functions.php
// Total Comment
function site_total_comment_count() {
$num_comm = get_comment_count();
$num_comm = $num_comm['total_comments'];
echo $num_comm ;}
add_shortcode('total_comment_count', 'site_total_comment_count');
// Total Completed Orders
function total_completed_Orders() {
$query = new WC_Order_Query( array(
'limit' => 99999,
'status' => array( 'completed' ),
'return' => 'ids',
) );
$orders = $query->get_orders();
return count( $orders ); }
// Copy to the desired page
<h2> All Orders:
<?php echo total_completed_Orders(); ?>
</h2>
<h2> All Comments:
<?php echo site_total_comment_count(); ?>
</h2>
<h2> All Posts:
<?php
echo $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'");
?>
</h2>
These codes work fine individually, but when I put all three on the target page, the stats show wrong.
Can you write me a code that shows the correct statistics of these three items from my site?
You can create a custom shortcode.
Try out this in your functions.php file:
add_shortcode('custom_shortcode', 'kb_get_details');
function kb_get_details()
{
//For total post.
global $wpdb;
$total_post = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'");
//For total comments.
$num_comm = get_comment_count();
$total_cmt = $num_comm['total_comments'];
//For total orders.
$query = new WC_Order_Query(array(
'limit' => 99999,
'status' => array('completed'),
'return' => 'ids',
));
$orders = $query->get_orders();
$total_orders = count($orders);
?>
<h2>All Posts : <?php esc_html_e($total_post); ?></h2>
<h2>All Comments: <?php esc_html_e($total_cmt); ?></h2>
<h2>All Orders : <?php esc_html_e($total_orders); ?></h2>
<?php
}
After that, this shortcode can be directly add it to your target page from the backend. You can also add it to any custom template by the use of do_shortcode('[custom_shortcode]');
Ok, so I know how to echo meta_key value for a post:
<?php
$meta_print_value=get_post_meta(get_the_ID(),'_heart_this',true);
echo($meta_print_value);
?>
I also know how to echo a TOTAL amount of posts with this specific meta_key:
<?php $query = new WP_Query( array( 'meta_key' => '_heart_this' ) )
echo $query->found_posts; ?>
What I need, is the TOTAL amount of posts for this meta_key for a post author.
I know I need to add get_the_author() somewhere in the above code to show the total amount of posts for the post author only, but struggling for a while now.
Any help would be appreciated.
Use Author Parameters in WP_Query(). You can use author id/name/multiple authors. For your case, the query should look like this:
<?php
$query = new WP_Query(
array(
'author' => get_the_author_meta( 'ID' );
'meta_key' => '_heart_this',
),
);
echo $query->found_posts; ?>
Ok, so right now I have a working code, that basically echo the total amount of posts for meta_key _heart_this for a specific user:
<?php $query = new WP_Query( array( 'meta_key' => '_heart_this', 'author' => 1, ) );
echo $query->found_posts; ?>
However, some posts have meta_value of 2, 3 etc..
So I want to sum the total amount of meta_value and echo, because right now the number I get is inaccurate obviously.
I'm almost there, need a bit of guidance.
Thanks in advance.
It should be taken in loop. Use below code
$query = new WP_Query( array( 'meta_key' => '_heart_this', 'author' => 1) );
//Take all values in array
$sum = [];
if( $query->have_posts()){
while( $query->have_posts()): $query->the_post();
{
//Get all posts values one by one
$value = get_post_meta( get_the_ID(), '_heart_this', true );
//Push values in array
array_push($sum, $value);
}
endwhile;
}
//Sum all values of array to get total amount of meta value
$sum_final = array_sum ($sum);
echo $sum_final;
Tested & works
I have created a custom field for my wordpress users: GENDER containing MALE and FEMALE.
Now I want to count the number of male and female users.
Best would be using functions.php
This code counts the total users. Can it be extended to count the genders?
add_shortcode( 'ucount', 'wpsites_user_count' ); //Count users
function wpsites_user_count() {
$count = count_users();
echo "There are " , $count['total_users'] , " on your website!";}
This does the job:
add_shortcode( 'countfemale', 'wpsites_user_countfemale' ); //Count users
function wpsites_user_countfemale() {
$args = array(
'meta_key' => 'gender', //any custom field name
'meta_value' => 'female' //the value to compare against
);
$users= new WP_User_Query( $args );
echo $users->get_total();
}
Ive been looking all over web i even tried to hire freelancer for help on this but had no luck. While searching i found this how to get popular posts fro selected categories in wordpress? & http://www.queness.com/code-snippet/6546/how-to-display-most-popular-posts-from-a-specific-category-in-wordpress and thats basically what i want but i want the information i get from it split up so i can rank the post.
<?php
$args=array(
'cat' => 3, // this is category ID
'orderby' => 'comment_count',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 6, // how much post you want to display
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { ?>
<ul>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php }
wp_reset_query(); ?>
With that code it gets the most popular post by comments and what i want to do is basically take the results and add a rank to it like example below.
#1 - post 1
#2 - post 2
#3 - post 3
#4 - post 4
#5 - post5 last post
thanks in advance for any help
May be this idea will help you.
Use get_comments_number( $post_id ) function
To get number of comment and then do a if else loop for displaying rank.
$num_comments = get_comments_number(); // get_comments_number returns only a numeric value
if ( comments_open() ) {
if ( $num_comments == 0 ) {
$rating= 0 ;
} elseif ( $num_comments > 1 ) {
$rating= 1 ;
} else {
$rating= 0 ;
}
}
Thanks
By your current question I understand the following:
You want to query from the WP database posts that have the most comments.
You want to display to visitors a ranking for those received posts. The ranking is determined by the amount of comments post has.
So your result could be:
1 Post A (comment count 500)
2 Post B (Comment count 499)
3 Post Z (Comment count 200)
This is how I would do it:
<?php
function get_popular_posts()
{
$sql="SELECT comment_count, guid AS link_to_post, post_title
FROM wp_posts
WHERE post_status = "publish" AND post_type = "post"
ORDER BY comment_count DESC
LIMIT 5"
return $wpdb->get_results($sql, OBJECT)
}
$post_objects = get_popular_posts();
$i = 1;
foreach($post_objects as $object)
{
echo 'Post: ' . $i . '#' . ' ' . $post_title . ' ' . $link_to_post . ' ' . $comment_count;
$i++;
}
?>
Haven't tested the code. But it should take five "top posts" from the database. Left in the comment_count for explanatory reasons.