Solved

I need some SQL help to pull some data out of my Wordpress database

Posted on 2013-01-08
3
490 Views
Last Modified: 2013-01-09
If you know what you're doing, this should be quite simple...

In English, I need a list of posts and the the file paths to the images associated with the posts.

There are two tables involved: wp_posts and wp_postmeta

The two types of relevant rows in wp_posts are "posts", which have the value of "publish" in the post_status column, and "images", which have the value of "inherit" in the post_status colum.

The values I need from the wp_posts table are the ID, post_date, and post_title

If an image is associated with a post, the post_parent column of the image's row will have the value of the ID column from the post's row.  (The post_parent value for a post is 0.)

The file path of the image is found in the meta_value column of the wp_postmeta table and is identified by having the value "_wp_attached_file" in the meta_key column.

The wp_postmeta table is linked to the wp_posts table by its post_id column.

What I'm try to ask is:
Give me all the post IDs, post_dates, and post_titles
Where the post has linked posts that are images (post_status = "inherit")
And while you're at it, give me the paths to those image files.

Hopefully, this is enough for someone who knows what they're doing to pull some decent SQL together for me.  If you need anything more to work with, just let me know.

Thanks!
0
Comment
Question by:sr-photo
  • 2
3 Comments
 

Author Comment

by:sr-photo
ID: 38757644
Any ideas?  I've been trying, but haven't come close.
0
 
LVL 24

Accepted Solution

by:
Tomas Helgi Johannsson earned 250 total points
ID: 38758847
Hi!

Try something like this

select p.id, p.post_date,p.post_title,m.*
from wp_posts p, wp_postmeta m
where p.post_id = m.post_id
order by p.id,p.post_date

You can then reduce the columns from wp_postmeta by replacing m.* with the desired columns.

Regards,
    Tomas Helgi
0
 

Author Closing Comment

by:sr-photo
ID: 38759584
Thank you,Tomas!  Your example gave me the insight needed for me to develop the SQL I was looking for.

Here is my query:

SELECT DISTINCT p.id, p.post_date, p.post_title, m.meta_value
FROM wp_posts p, wp_posts p2, wp_postmeta m
WHERE p2.post_parent = p.id
AND m.post_id = p2.id
AND m.meta_key = "_wp_attached_file"
ORDER BY p.id, p.post_date

I had forgotten that I could use aliases to link rows from the same table and that was the missing piece.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Force inline or simulate using css on a pdf 7 44
css question 1 33
modify h2 4 19
Having multiple API's on a site 10 16
How to create your own WordPress Theme Before I begin with the theme design tutorial, I would like to give you the basics of what is required to make a WordPress Theme. The most basic WordPress theme only requires 2 files: a style.css file and an…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to create a Printer Friendly PDF on a WordPress Page. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome Screenshot” Google Chrome Extension, and SmallPDF.com Log…
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now