Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-01-08
3
Medium Priority
?
509 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 750 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
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…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. 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 : Go t…

721 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