Solved

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

Posted on 2013-01-08
3
497 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 25

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…

828 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