2

I am trying to return the number of reviews for a given page ID, I have this so far:

<?php
global $wpdb;
$countreviews = $wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM $wpdb->wpcreviews     WHERE page_id = ID"));
echo 'Number Of Reviews ' . $countreviews . '';
?>

Its not working and I assume it's the end part page_id = ID

How do I change the ending to that it searches the wpcreviews table for items with the current page ID, which is the same as my post ID?

1 Answer 1

2
$countreviews = $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(ID) FROM ' . $wpdb->prefix . 'wpcreviews WHERE page_id = %d', get_the_ID() ) );
2
  • Will this work outside of the loop as well? Perhaps get_queried_object_id() might be better.
    – Bendoh
    Sep 21, 2012 at 1:18
  • it'll work on a single post/page, it pulls from the global $post, which is populated with the first query result before calling the_post().
    – Milo
    Sep 21, 2012 at 2:41

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.