I have a custom post type "events" which includes the custom fields "is_premium" (which is a true/false) and "premium_expiry_date" which is a date picker.

On my front end, I want to have "events" which are premium and their premium_expiry_date has not passed to be displayed on the top of the results.

I have managed to do half of that by WP_Query querying "events" for which the is_premium==true && premium_expiry_date>$today (my code shown below). I pull them successfully in a table which appears ton top of my page.

My problem is with displaying the rest, and avoiding double posts.

Essentially, I would need a query like [ is_premium==false OR (is_premium==true AND premium_expiry_date<$today ) ] but I know that AND and OR cannot be mixed in WP_Query.

I have tried to go into using $wpdb but with no luck (I am lacking SQL knowledge...)

My question is, could somebody either help me with how to syntax this using $wpdb, or direct me to a different path for achieving my desired result?

        // This is the code I use to show the premium events which have not occurred yet,
        // and their premium date has not passed
        $today = date('Ymd');
        $args = array(
        'post_type' => 'event',
        'orderby'   => 'event_date',
        'meta_key'  => 'event_date',
        'order' => 'ASC',
        'relation' => 'AND',
        'meta_query' => array(

                'key'       => $the_key, //I read this from a _GET value. It resembles another custom field
                'compare'   => 'LIKE',
                'value'     => $current_region->name, //filter based on current region

                'key'       => 'event_date',
                'compare'   => '>=',
                'value'     => $today

                'key'       => 'is_premium',
                'compare'   => '==',
                'value'     => 1

                'key'       => 'premium_expiry_date',
                'compare'   => '>=',
                'value'     => $today



    <?php $the_query = new WP_Query( $args ); ?>

    <?php if( $the_query->have_posts() ): ?>

    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

... END 

Thanks a lot in advance


This is the output of <?php var_dump($GLOBALS['wpdb']->last_query) ?> when placed right after <?php $the_query = new WP_Query( $args ); ?>

string(97) "SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (147) ORDER BY meta_id ASC"
  • Could you file an edit and show us a var_dump() of $GLOBALS['wpdb']->last_query right after your WP_Query?
    – kaiser
    Jun 13, 2014 at 9:03
  • Take a look at this WP_Query multiple use of relation and/or Jun 13, 2014 at 9:42
  • @kaiser added the output, is this what you asked?
    – andreas
    Jun 14, 2014 at 3:53
  • @andreas yes this it :) Sidenote: Are you sure that the code in after "after" at "when placed right after" is the right one?
    – kaiser
    Jun 14, 2014 at 12:16
  • took me a while to understand what you just said haha... but yeah that was a typo - edit / fixed. I still don't understand much of the output though...
    – andreas
    Jun 14, 2014 at 12:25


Your Answer

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

Browse other questions tagged or ask your own question.