Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1 Answer 1

$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

 
discard

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.