I'm trying to get the simplest Wordpress database function to return a result and it shows up blank. The query works fine in phpMyAdmin and returns the result I want, but not via a page template in Wordpress.
This function is running on a page template that's based off the twentythirteen theme page.php
(I simply replaced the the_content()
with my function). Code Below:
<?php
function ag_get_count_to_work(){
global $wpdb;
$num_posts = $wpdb->get_var("SELECT count(*) FROM wp_posts");
if ($num_posts) {
echo "<h1>" . $num_posts . "</h1>";
} else {
die(mysql_error());
}
}
?>
I might not have the if
statement setup correctly, but even trying without the if
statement (i.e., JUST return the result), the result is always blank, and there are no errors shown. Can some eagle-eyed developer tell me what I'm missing please?
This is not the final function I want, but I'm just trying to break the problem down to its origin and this might be it.
Turns out the function was defined but not called. Probably because I'm so used to putting functions in the functions.php file. Once it was called it seems to work fine.