Solved

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

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

 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…

632 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