WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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 its 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?

share|improve this question
$countreviews = $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(ID) FROM ' . $wpdb->prefix . 'wpcreviews WHERE page_id = %d', get_the_ID() ) );
share|improve this answer
Will this work outside of the loop as well? Perhaps get_queried_object_id() might be better. – Bendoh Sep 21 '12 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 '12 at 2:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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