I have been trying to get this to work for awhile now and I cannot seem to get any farther on my own. It seems to work fine as a normal SQL query but not within the WP framework using prepare(). Basically I am trying to insert a PHP variable within the SQL query but instead of returning the value I SELECTed, the function returns the value of the variable.
I have searched for others having the same problem but most of them are dealing with problems with LIKE and the % wildcard. I cannot figure out what my problem is.
I am using a bbPress variable which is the forum id.
Any help would be appreciated.
global $wpdb;
$countquery = $wpdb->get_var($wpdb->prepare(
"SELECT meta_value FROM $wpdb->postmeta
WHERE meta_key = '_bbp_forum_subforum_count'
AND post_id=%d", bbp_forum_id()));
echo $countquery;
var_dump( $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_bbp_forum_subforum_count' AND post_id=%d", bbp_forum_id() ) );, does it look the way you expect? Doesbbp_forum_id()return a number, or a string that contains a number? If the latter, try using(int) bbp_forum_id().518string(93) "SELECT meta_value FROM cd_postmeta WHERE meta_key = '_bbp_forum_subforum_count' AND post_id=0"