Skip Navigation

[Resolved] SQL SELECT Query

This support ticket is created 7 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by julieP 7 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#457961

I'm looking to export a few fields from my custom posts (CSV file would be fine):-

post_title and post_type from wp_posts and the values for custom fields wpcf-total-charge, wpcf-deposit-paid and wpcf-balance-due from wp_postmeta.

I thought the simplest way would be to use an SQL statement in phpmyadmin. However, the custom fields need to act as column headings but in the database are themselves a value under 'meta-key' so I'm not clear how to go about this. (I'm not keen on installing yet another plugin for such a small task).

Any ideas please?

#458227

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Julie

There are an abundance of plugins that can do that for you, but if you'd rather DIY with a direct database query you will need to learn some SQL.

If you want to skip reading through tutorials etc., the format of your query would look something like this...

SELECT p.ID, p.post_title, p.post_type, pm1.meta_value, pm2.meta_value
FROM wp_posts p
JOIN wp_postmeta pm1 ON pm1.post_id = p.ID
JOIN wp_postmeta pm2 ON pm2.post_id = p.ID
WHERE 
pm1.meta_key = 'wpcf-slug1'
AND
pm2.meta_key = 'wpcf-slug2'

You should hopefully be able to see how to expand that to other custom fields.

#459017

That's fantastic - thank you so much! I'd been trying to achieve what I wanted using Views but it was a bit long-winded - this is much better. Great support as usual 🙂

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.