1

I have searched all through the forums and WP docs, but can't find an answer to whether or not WP Meta Query permits a sub meta query. My problem is this: I have a number of types of promos (custom post type) with start and end dates(stored as unixtime in the db). I want to exclude one type of promo ("walls", which is working fine) and also (using OR for either case) exclude items whose start dates AND end dates (IF they exist) are in the past. Here is my code:

$nowtime = time();
$the_query = new WP_Query( array(
'post_type' => 'promos', 'orderby' => 'menu_order', 'order' => 'ASC', 'meta_query' => array(
    'relation' => 'OR',
array(
    'key'     => 'wpcf-type-of-promo',
    'value'   => 'walls',
    'compare' => 'NOT LIKE'
),
array(
'relation' => 'AND',
array(
    'key'     => 'wpcf-date-and-time-start',
    'value'   => $nowtime,
    'compare' => '<',
    'type' => 'NUMERIC'
),
array(
    'key'     => 'wpcf-date-and-time-end',
    'value'   => $nowtime,
    'compare' => '<',
    'type' => 'NUMERIC'
))
) ));

For the above code, the promo type of "walls" is correctly being filtered out, but the start dates and end dates in the sub query are being completely ignored.

1

I don't think you need the relation parameter. It defaults to AND.

    $nowtime = time();
    $the_query = new WP_Query( array(
     'post_type' => 'promos', 
     'orderby' => 'menu_order', 
     'order' => 'ASC', 
     'meta_query' => array(
         array(
          'key'     => 'wpcf-type-of-promo',
          'value'   => 'walls',
          'compare' => 'NOT LIKE'
         ),
        array(
          'key'     => 'wpcf-date-and-time-start',
          'value'   => $nowtime,
          'compare' => '<',
          'type' => 'NUMERIC'
        ),
        array(
          'key'     => 'wpcf-date-and-time-end',
          'value'   => $nowtime,
          'compare' => '<',
          'type' => 'NUMERIC'
        )
     )
   ));
  • Thanks, I think this is a little closer, but my problem is still that if "wpcf-date-and-time-end" is empty I need to account for that, and this query does not. So, for example, if I have an event that has no end date, I still need to check that it is empty and only use the start date for my expiration. Hope that makes sense. – Stephen Nov 20 '14 at 0:07
  • And actually, I need the times to be a subquery I think, because I have to check: 1) that both times are in the past and 2)Ignore the wpcf-date-and-time-end field if it is empty. I guess I can force people to fill in both fields, but if the wpcf-date-and-time-end is a later date, I still need to show the item, which this query will hide. – Stephen Nov 20 '14 at 0:16
  • Then do an if / else statement to build the appropriate args. Then do $the_query = new WP_Query( $args ); – shanebp Nov 20 '14 at 14:57
  • I don't see how that would work, since I need to query first to find out the info I would need to build those args. – Stephen Nov 23 '14 at 22:08
0

I have a workaround that goes like this:

I run my query above, and only filter on the type and END date, which reduces the returned set somewhat.

THEN, in my loop, I add some PHP logic that basically says:

IF ((end-date is not empty) AND (end-date is less than now) AND (start-date is less than now)) do nothing. Otherwise, show the post.

This works for now, but it would be great if there was a query that would return exactly what I want from the outset.

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.