Using custom sql query in wp_query function

Sponsors

Many people asked me about fetching data from wordpress database with simple database query. Is this facility provided by wordpress API. Yes, that is provided by wordpress api. You just need to use the get_results() method.

Here I am going to give some example about using the custom sql query in wp_query method.

You need to define the gobal $wpdb variable that important in your code. here is working example for fetching the posts.


global $wpdb;

$querystr = "SELECT wp_posts.* FROM $wpdb->posts wp_posts, $wpdb->postmeta
WHERE wp_posts.post_status = 'publish' AND wp_posts.post_type = 'post'
ORDER BY wp_posts.post_date DESC LIMIT 10";
$pageposts = $wpdb->get_results($querystr, OBJECT);

For fetching the Admin or user information thorough query use following code. following code is useful for fetching the single record or row.


$querystr = "SELECT wp_users.* FROM $wpdb->users
WHERE wp_users.user_email = 'info@domain.com' ";
$userinfo = $wpdb->get_row($querystr);

For detail information you should use the following URL:

http://codex.wordpress.org/Function_Reference/wpdb_Class

Using custom sql query in wp_query function

Using custom sql query in wp_query function

Incoming search terms:

You may like following Articles!

This entry was posted in wordpress by Wordpress API. Bookmark the permalink.

About Wordpress API

API means Application Program interface. In Wordpress API we are giving all the information about wordpress API. We worked for many wordpress plugins and themes. We have expertise in PHP, Databases, Graphic Design, Ruby on Rails, Java, Wordpress, Drupal and many CMS. We always give you the best solutions about wordpress and other related topics in This Blog. We will give you always fresh tips and tricks and techniques about wordpress in this blog.

4 thoughts on “Using custom sql query in wp_query function

  1. Nice post! You truly have a wonderful way of writing which I find captivating! I will definitely be bookmarking you and returning to your blog. In fact, your post reminded me about a strange thing that happened to me the other day. I’ll tell you about that later…

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>