Every post has two post meta: year and month (custom)
I need SQL query to pull all posts that have
wp_postmeta.meta_key = year AND wp_postmeta.meta_value = 2013
AND AT THE SAME TIME another meta_key / meta_value pair
wp_postmeta.meta_key = month AND wp_postmeta.meta_value BETWEEN 1 AND 12 ;
SELECT * FROM wp_postmeta
WHERE (meta_key = 'agam_post_options_year' AND meta_value = 2013)
OR (meta_key = 'agam_post_options_month' AND meta_value BETWEEN 0 AND 12 )
GROUP BY meta_value;
This is something I tried and a couple of more variations but it doesn’t do much… this specifically gives me one post that has year 2013 and 12 posts that have either 1 or … to 12 for month field}
Please replace _PUT_YOUR_POST_ID_FIELD_HERE_ by something appropriate field from that table and try to run…
OK It was all good. The reason it took out the “duplicates” was because we didn’t group by Y.meta_id and instead we used meta_value
If you don’t really need a pure SQL query and you’re getting these posts for using them in WordPress, you could use meta_query array passed as a part of args to WP_Query class or get_posts() function.
Hope it helps! : )