I am fairly new and I am struggling with a simple WordPress SQL query on the standard database for a plugin I am developing.
I am using the following code to echo out the titles of the first 10 posts:
global $wpdb;
$results = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->posts LIMIT 0, 10;"));
$i = 0;
while($i < count($results)){
echo $results->post_title;
$i++;
}
But nothing is getting echo’d out to the screen. There are a more that 10 posts in the database so not having data is not the issue.
I believe instead of this:
(Which will always echo the same variable), what you need to do is this:
Because
$results
is an array.You might be able to do this as well, but there’s no benefit over
foreach
: