Your SQL query is invalid regarding the WP database structure.
wp_postmeta
table has two columns that will help you here.
meta_key
: the name of your meta (post_lang
in your case)
meta_value
: the value of your meta (en
, es
, …)
Your SQL query should look like this instead.
What changed is that you need to query the wp_postmeta.meta_key
column to match your meta name and wp_postmeta.meta_value
to match your language.
SELECT p.ID
FROM wp_posts AS p
INNER JOIN wp_postmeta AS pm
WHERE 1=1
AND (p.post_title = %s)
AND (p.post_type="especialidades")
AND (pm.meta_key = 'post_lang')
AND (pm.meta_value = %s)
You could also add the JOIN
condition to retrieve only the rows that you need.
SELECT p.ID
FROM wp_posts AS p
INNER JOIN wp_postmeta AS pm ON (p.ID = pm.post_id AND pm.meta_key = 'post_lang' AND pm.meta_value = %s)
WHERE 1=1
AND (p.post_title = %s)
AND (p.post_type="especialidades")
You can try to run this query in your database management tool to see how it works. You can also add a more “human friendly” SELECT
clause to better understand the links between the tables.
SELECT p.ID, p.post_title, pm.meta_key, pm.meta_value