Solved

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

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

WordPress is constantly evolving, and with each evolution appears to get better and better.  One of the big drawbacks prior to version 3 was that there was no way to be able to set up a custom menu from the backend. The Old Way Adding menus is…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

810 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