How To Get Some Data From WordPress Database Using WordPress $wpdb Query?

I am trying to get some data from WordPress database tables in a plugin. For that, I am using the below code…

global $wpdb;
$findID = $wpdb->get_var("SELECT ID FROM wp_posts WHERE post_name="hello-world"");
echo $findID;

But it not giving me the post ID in echo? Is there anything wrong…???

2 Answers
2

Just to clarify the get_var() method of $wpdb does work just fine in this context:

global $wpdb;
$helloworld_id = $wpdb->get_var("SELECT ID FROM wp_posts WHERE post_name="hello-world"");
echo $helloworld_id;

Actually it is more practical in this context, because a single variable is returned, which is what is actually wanted.

Leave a Comment