EM post meta are hidden ones, so they start with _.
Try _event_start_date
I modifid the code to this:
function my_search_query( $query ) {
// not an admin page and is the main query
if ( !is_admin() && $query->is_main_query() ) {
if ( is_search() ) {
$query->set('orderby', '_events_start_date');
$query->set('order', 'DESC');
return $query;
}
}
}
add_action( 'pre_get_posts', 'my_search_query' );
But not working well, here is the page: https://tinyurl.com/y6qvny2c
The 3rd and 4th results should be reversed (3rd is 2019.07.20, 4th is 2019.09.05)!
Check your spelling. You are using plural which is wrong
Thanks, I deleted the ‘s’ , but the result is the same. :/ I think the _event_start_date not the right parameter.
you can try _start_ts
e.g.
function my_em_wp_query(){
$args = array(
'post_type' => 'event',
'posts_per_page' => 100,
'meta_query' => array( 'key' => '_start_ts', 'value' => current_time('timestamp'), 'compare' => '>=', 'type'=>'numeric' ),
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => '_start_ts',
'meta_value' => current_time('timestamp'),
'meta_value_num' => current_time('timestamp'),
'meta_compare' => '>='
);
// The Query
$query = new WP_Query( $args );
// The Loop
while($query->have_posts()):
$query->next_post();
$id = $query->post->ID;
echo '<li>';
echo get_the_title($id);
echo ' - '. get_post_meta($id, '_event_start_date', true);
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
}
add_shortcode('em_wp_query','my_em_wp_query');
-
This reply was modified 6 years, 9 months ago by
angelo_nwl.