user2093301
user2093301

Reputation: 367

Display only future events (cpt)

My current code shows 5 posts from the cpt 'events'. This is the code i'm using:

//get post type ==> events
global $post;
$args = array(
      'post_type' => 'events',
      'numberposts' => $data['home_events_count'],
      'orderby' => 'meta_value',
      'meta_key' => 'timestamp_earth_event_start_date',
      'meta_query' => $meta_query
);

I want to filter out posts (events) that their 'timestamp_earth_event_start_date' are older than the current date.

I've tried to modify the code myself, but still past events are shown.

Any help would be most appreciated!

Upvotes: 0

Views: 179

Answers (1)

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

Try this one passing query string

$numposts=$data['home_events_count'];
    query_posts('post_type=events
    &post_status=publish&numberposts=$numposts
    &posts_per_page=1
    &meta_key=timestamp_earth_event_start_date
    &orderby=meta_value_num
    &order=DESC')

By passing array

$args = array(
    'post_status' => 'publish',
    'post_type' =  'events' ,
    'meta_key' => 'timestamp_earth_event_start_date',
    'order' => 'DESC',
    'orderby' => 'meta_value_num'
);
$event_posts = new WP_Query( $args );

Other option you can add filter for the query like

add_filter( 'posts_orderby', 'my_posts_orderby_date', 10, 2 );
function my_posts_orderby_date( $orderby, $query ) {
    global $wpdb;
    return " CAST( $wpdb->postmeta.meta_value AS DATE ) " . $query->get( 'order' );
}

Upvotes: 1

Related Questions