WordPress Query by Meta key, type date numeric

Ii have problem with WP Query, can’t order by meta key
I use Event Manager plugin, and need query 4 post on my Homepage,
I need post only current and future events(posts) and sort by start date.
Here is my code

$time = date('Y-m-d');
$timeStro = strtotime($time);
$myNewQuery = new WP_query(
        array(
                    'post_type' => 'event',
                    'featured' => 'yes',
                    'meta_key' => 'event_start_date',
                    'meta_type' => 'numeric',
                    'orderby' => 'meta_value_num',
                    'order' => 'ASC',
                    'showposts' => 4,
                    'meta_query' => array(
                            array(
                                    'key' => strtotime('_event_end_date'),
                                    'value' => $timeStro,
                                    'type' => 'numeric',
                                    'compare' => '>='
                                )
                        )

            ));

Thanks you all! 🙂

Read More

At the end, we must hardcode and make our query from DB

       <?php
           global $wpdb;
           $time = date('Y-m-d');
           $events = $wpdb->get_results( "SELECT * FROM wp_em_events WHERE     event_end_date >= '$time' ORDER BY event_start_date LIMIT 4");

  foreach($events as $event) {
?>
            <li>
                <a href="<?php echo get_permalink($event->post_id); ?>">
                    <div class="div-calendar-entry">
                        <p class="calendar-title"><?php echo $event->event_name; ?></p> 
                    </div>
                </a>
            </li>
<?php } ?>

Related posts

2 comments

  1. please use

    'key' => ('metakey_slug')
    

    in place-of

    'key' => strtotime('_event_end_date')
    
  2. I ended up with the following:

    <?php
    global $wpdb;
    $today = date( 'Y-m-d' );
    $upcoming_events = $wpdb->get_results( "SELECT post_id FROM wp_em_events WHERE event_start_date >= '$today' ORDER BY event_start_date LIMIT 100" );
    if ( $upcoming_events ) {
        $upcoming_events_post_ids = wp_list_pluck( $upcoming_events, 'post_id' );
        $upcoming_events_args     = [
            'post_type'      => 'event',
            'post__in'       => $upcoming_events_post_ids,
            'orderby'        => 'post__in',
            'posts_per_page' => 100,
        ];
        $upcoming_events_query    = new WP_Query( $upcoming_events_args );
        if ( $upcoming_events_query->have_posts() ) {
            while ( $upcoming_events_query->have_posts() ) :
                $upcoming_events_query->the_post();
                get_template_part( 'template-parts/event' );
            endwhile;
            wp_reset_postdata();
        }
    }
    ?>
    

Comments are closed.