Understanding Wordpress database schema - querying from 3rd party app
Is there an easy way to grab the latest posts out of a Wordpress wp_posts
table using a simple SQL query?
I have a Wordpress 2.9.2 installation as part of, but separate from, a larger system. It has a customized theme to look like the rest of the site but has otherwise nothing to do with it. I want to display the latest handful of headlines of posts made using Wordpress on a site of that other system. Preferably I do not want to mess around with importing any of the Wordpress library files.
Looking at the database structure I can't see an easy, straight-forward query to simply get the latest revision of the latest posts. The post_status
can either be "post" or "inherit", the post_type
"post" or "revision" and the parent "0" or the id of the original post of a revision. I can't figure out how to reliably filter different revisions of开发者_如何转开发 the same post, drafts, attachments and pages out of this mess and just get the latest revision of the latest posts.
I'm aware that the database schema is subject to change in subsequent versions of Wordpress, so shouldn't be relied upon, but that's a minor concern, since it's such a minor feature that could easily be fixed. If I understood how that database is supposed to work, that is.
You're looking for all posts where post_status = publish and post_type = post.
The WordPress posts table also includes pages, revisions, and attachments ... that's why you need to be specific.
If you're using the default database scheme (with wp_
as a table prefix), the following query will give you what you need:
SELECT post_title FROM wp_posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC
This will give you a list of the most recent headlines (post titles) of published posts ordered by date (with the most recent at the top). No need to import any core WordPress libraries. Just make sure not to accidentally tweak the database while accessing it directly or you might break something in WordPress.
精彩评论