0

I have a custom post type called "Event", which contains a repeatable date field _cmb2_date . I have a function which prints out posts:

function post_style_6($args) {
    global $post;
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) :
    while ( $the_query->have_posts() ) : $the_query->the_post();
        $date = get_post_meta( $post->ID, '_cmb2_date', true );
        foreach ($datum as $day):
            if(!empty($day)) {
                echo date('d.m.Y', $day);
            }
         endforeach;
    endwhile;
    wp_reset_postdata()
    endif;
}

and here is how I call the loop:

$today = getdate();
post_style_6(
    array(
        'posts_per_page'   => 1,
        'offset'           => 0,
        'orderby'          => 'meta_value',
        'order'            => 'ASC',
        'meta_key'         => '_cmb2_date',
        'meta_value'       => strtotime($today['year'].'-'.$today['mon'].'-'.$today['mday']),
        'meta_compare'     => '<=',
        'post_type'        => 'mb_event_post',
        'post_status'      => 'publish',
        'suppress_filters' => true
        )
    );

This solution was working until I made the date field repeatable using Custom MetaBoxes 2. The problem is the meta_key _cmb2_datum is an array which holds the time as a Unix timestamp.

Why do I need the date repeatable? Some events will have multiple times or dates, and I need to select the closest one, or the closest three from category "Movie", etc.

How do I select the closest events in the future?

  • 1
    it's possible, but not easy. you may be better off rethinking how you're storing that data. see meta_query with meta values as serialize arrays. – Milo Feb 16 '15 at 22:43
  • If the timestamps were like 2015-2-15, you could do a LIKE query – Manny Fleurmond Feb 17 '15 at 6:30
  • I have no idea how to achieve that with unserialize.. Also I have doubts about the LIKE method :-/ I'm still PHP beginner. – 5ulo Feb 17 '15 at 9:37

Your Answer

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

Browse other questions tagged or ask your own question.