WordPress is really a force to be reckoned with as far as CMS go. With a huge community behind it, a plethora of features and frequent updates, it has become the defacto CMS to use. Other than a robust admin section, it’s also very easy to create templates for the front end as well.

Having said the above however, there might arise a need to query the WordPress database directly. Suppose you need to create a jQuery Mobile website using something other than php and you need to tap into the WordPress CMS for the posts.

An easy way to do this is to create a custom view within the WordPress database. The main table that houses the posts in WordPress is called “wp_posts”, and together with its related tables we can get all the data we need:

And here’s the SQL code:

The query looks for all posts that have been marked as published, with categories that have slug “news”, “videos”, or “photos”. You can certainly add more arguments to the query, like the “post_date”  and so on. It all depends on your needs.

We can then save the SQL code above into a view of its own (let’s call it “view_all_posts”), and run a simple query against it to select the latest 10 posts:

Once we have our custom query, we can simply loop through it using our application language and output the content the way we want it.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.