2

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}

4
  • 1
    Could you show us what you have tried so far? Where's your code? Commented Nov 11, 2014 at 9:05
  • Thanks for adding your code. Edit your original post and add the code there instead of adding a comment :). Commented Nov 11, 2014 at 9:12
  • OK, do you think you can help me ? :) Commented Nov 11, 2014 at 9:33
  • If you want to filter data for both conditions, you should use AND instead of OR, because you'll get any rows from 2013 or any month (between 1 and 12) which should return the whole table. Or maybe I didn't understand exactly what you're lookin' for... Commented Nov 11, 2014 at 9:40

3 Answers 3

1

Please replace _PUT_YOUR_POST_ID_FIELD_HERE_ by something appropriate field from that table and try to run...

SELECT Y.meta_value, M.meta_value FROM wp_postmeta as Y
JOIN wp_postmeta AS M USING (_PUT_YOUR_POST_ID_FIELD_HERE_) 
WHERE (Y.meta_key = 'agam_post_options_year' AND Y.meta_value = 2013)
AND (M.meta_key = 'agam_post_options_month' AND M.meta_value BETWEEN 0 AND 12 )
GROUP BY Y.meta_value, M.meta_value; 
Sign up to request clarification or add additional context in comments.

1 Comment

OK I will be marking this as the correct answer. The only thing I did was modified it a bit SELECT Y.*, M.* FROM wp_postmeta as Y JOIN wp_postmeta AS M USING (post_id) WHERE (Y.meta_key = 'agam_post_options_year' AND Y.meta_value = 2013) AND (M.meta_key = 'agam_post_options_month' AND M.meta_value BETWEEN 0 AND 12 ) GROUP BY Y.meta_value, M.meta_value ORDER BY M.meta_value+0 DESC So I get that DESC order.. however.. I noticed that it does not duplicate results... I had two posts with the same year and same month... now I don't see it... is there anything there that's preventing this ?
0

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

Comments

0

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.

$args = array(
 'posts_per_page' => -1,
 'post_type'      => 'your-post-type', // default 'post'
 'meta_query'     => array(
      relation => 'AND', // default 'AND' it might be 'OR'
      array(
          'key'   => 'agam_post_options_year',
          'value' => '2013',
          'compare'=> '=',
          'type'  => 'NUMERIC' // see docs for more types
      ),
      array(
          'key'   => 'agam_post_options_month',
          'value' => array( 0, 12 ),
          'compare' => 'BETWEEN',
          'type'  => 'NUMERIC' // see docs for more types
      )
   )
);

$posts = get_posts( $args ); // returns an array of posts objects
//OR
$query = new WP_Query( $args ); // returns a new WP_Query object

Hope it helps! : )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.