0

I am trying to select post ids and a post meta value based on the value of 2 other post meta values and from a custom post type.

Here is a screen shot of my post meta table. https://www.evernote.com/shard/s40/sh/99a4c115-48bb-46c4-81ef-0df3ccd48862/3f9e2d56e7bb5db54a64782b9860ab4b/deep/0/wp_13_postmeta%20@local_wp_multisite_%20(localhost).png

I need the _days meta_key value based on the values of the _start_date and _end_date values.

I also would like to return the post id's.

UPDATE: I think I got it. It is not in wordpress form but that is the easy part. If anyone can improve on this please add to it. Thanks

 SELECT
key1.ID,
key1.post_title,
days.meta_value AS days
FROM
wp_13_posts key1
INNER JOIN wp_13_postmeta days ON days.post_id = key1.ID
AND days.meta_key = '_days'
INNER JOIN wp_13_postmeta startdate ON startdate.post_id = key1.ID
AND startdate.meta_key = '_start_date'
INNER JOIN wp_13_postmeta enddate ON enddate.post_id = key1.ID
AND enddate.meta_key = '_end_date'
WHERE
days.meta_key = '_days'
AND startdate.meta_value <= '1385769600'
AND enddate.meta_value >= '1383264000'
AND key1.post_type = 'mbe_enrollments'

1 Answer 1

0
 $post_this_month = $wpdb->get_results($wpdb->prepare("
    SELECT
    key1.ID,
    key1.post_title,
    days.meta_value as days
    FROM
    $wpdb->posts key1
    INNER JOIN $wpdb->postmeta days ON days.post_id = key1.ID
    AND days.meta_key = %s
    INNER JOIN $wpdb->postmeta startdate ON startdate.post_id = key1.ID
    AND startdate.meta_key = %s
    INNER JOIN $wpdb->postmeta enddate ON enddate.post_id = key1.ID
    AND enddate.meta_key = %s

    WHERE days.meta_key = %s
     AND startdate.meta_value <= %s
     AND enddate.meta_value >= %s
     AND key1.post_type = %s
      ", '_days', '_start_date', '_end_date',
    '_days', $month_end, $month_start, 'mbe_enrollments'
    ));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.