Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Need to show CPT count based on custom field WordPress

  • SOLVED

I have a custom post type where a user can specify a star rating.

I want to print out the number of custom post types that have a 5 star rating, the number that have a 4 star rating, etc.

With this code I can show a general count:


<?php $reviews = new WP_Query(array( 'post_type' => 'reviews' ));?>
<?php if ($reviews->have_posts()) {

$count_posts = wp_count_posts( $type='reviews' )->publish;
echo "$count_posts 5 Star Reviews";

} else { ?>
0
<?php } ?>


But this is just showing a count for all of the reviews. I need to know how to make it sort based on my custom field, which is star_rating.

Thanks!

Answers (3)

2015-03-16

Arnav Joy answers:

so post will have custom field "start_rating" and its value be 5 for a post whose review is 5? and value will be 4 for a post whose review is 4 .. and so on...?


Arnav Joy comments:

please try this code to get 5 star posts

<?php

global $wpdb;

$rating_5 = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->postmeta WHERE meta_key='start_rating' AND meta_value=5" );


Kyler Boudreau comments:

Yes


Arnav Joy comments:

have you tried my code ?


Kyler Boudreau comments:

Arnav,

Sorry dude....got distracted with stuff. Just now back on this. I couldn't get your code to work.

2015-03-16

Dbranes answers:

You could try:

$reviews = new WP_Query(
array(
'fields' => 'ids',
'post_status' => 'publish',
'post_type' => 'reviews',
'posts_per_page' => 1,
'meta_query' => array(
array(
'key' => 'star_rating',
'value' => '5',
'compare' => '=',
)
)
)
);

echo $five_stars = $reviews->found_posts;


where the <em>found_posts</em> attribute returns the total number of posts found matching the query arguments.

We don't need to return more than a single post id, to get the <em>found_posts</em> value, so therefore I added <em>posts_per_page = 1</em> and <em>fields = ids</em>.

You can now easily modify this query to your needs.

2015-03-16

Kyle answers:

Building on Dbranes solution, you may want to try to making the query into a function since you are using it more than once, by passing the star rating as an argument.

So using his code turned into a function:


function get_cpt_review_count( $stars ){

$reviews = new WP_Query(
array(
'fields' => 'ids',
'post_status' => 'publish',
'post_type' => 'reviews',
'posts_per_page' => 1,
'meta_query' => array(
array(
'key' => 'star_rating',
'value' => $stars,
'compare' => '=',
)
)
)
);

return $reviews->found_posts;

}



Then inside your templates or where you want to show the information you could do this:

echo 'There are '.get_cpt_review_count("5").' reviews with 5 stars';
echo 'There are '.get_cpt_review_count("4").' reviews with 4 stars';
echo 'There are '.get_cpt_review_count("3").' reviews with 3 stars';


Kyler Boudreau comments:

Kyle - couldn't get this to work. I'm going to vote the money to you and Dbranes, but for some reason it's not working form me.