Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL and WordPress: Extract two custom fields with SQL

Tags:

sql

php

wordpress

WordPress wp_postmeta table is constructed as a key/value pair:

ID | meta_key | meta_value

I need to get the values of two meta_keys within the result set:

property_lat and property_long

I will then use these values to assess the radius of a given point.

like image 805
dcolumbus Avatar asked Sep 17 '26 22:09

dcolumbus


1 Answers

Two values in one result set:

SELECT * 
FROM wp_postmeta 
WHERE meta_key IN ('property_lat', 'property_long')

Two values in one row:

SELECT (SELECT meta_value FROM wp_postmeta WHERE meta_key='property_lat' LIMIT 1) AS lat, 
       (SELECT meta_value FROM wp_postmeta WHERE meta_key='property_long' LIMIT 1) AS lng
like image 200
peterm Avatar answered Sep 19 '26 12:09

peterm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!