Last active
January 12, 2018 04:15
-
-
Save shulard/1eda03e0115755890b76d5998bc42643 to your computer and use it in GitHub Desktop.
WordPress: SQL Query to retrieve results when meta_key like `modules%_module` and meta_value as a given value...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
global $wpdb; | |
$myid = 157; | |
$views = $wpdb->get_results($wpdb->prepare( | |
'SELECT p.ID | |
FROM `wp_posts` as p | |
LEFT JOIN `wp_postmeta` as m ON p.ID=m.post_id | |
WHERE m.meta_id IN ( | |
SELECT meta_id FROM wp_postmeta | |
WHERE meta_key LIKE "modules%%_module" | |
) AND m.meta_value=%s', [$myid] | |
)); | |
//$views contains all the posts ID which are linked to a meta_key named like `modules%_module` and with a value equal to $myid... | |
foreach ($views as $v) { | |
$post = get_post($v->ID); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT p.ID | |
FROM `wp_posts` as p | |
LEFT JOIN `wp_postmeta` as m ON p.ID=m.post_id | |
WHERE m.meta_id IN ( | |
SELECT meta_id FROM wp_postmeta | |
WHERE meta_key LIKE "modules%_module" | |
) | |
AND m.meta_value=157; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment