0

Hey i have the following query sorting custom agendas posts on an archive page based on a metabox for the event date.

$args = array(
  'post_type'       =>  'agendas',
  'posts_per_page'  =>  $posts_per_page,
  'paged'           =>  $paged,
  'meta_key' => '_agenda_date_agenda',
  'order'           => 'ASC',
  'orderby' => 'meta_value',
  'meta_query' => array(
   array(
       'key' => '_agenda_date_agenda',
       'value' => strtotime('now'),
       'compare' => '<',
   )

)

The query does only take the day and month but not the year, so if for example an agenda has a date october 10 2018, he will be sorted before an agenda for december 2017... any idea how to solve ? thanks a lot

edit here's the metabox code

add_filter( 'cmb2_admin_init', 'agenda_settings' );

function agenda_settings( ) {

// Start with an underscore to hide fields from custom fields list
$prefix = '_agenda_';

$cmb = new_cmb2_box( array(
    'id'            => $prefix . 'settings',
    'title'         => __( 'date', 'compassion' ),
    'object_types'  => array( 'agendas' ),
) );
$cmb->add_field( array(
    'name'      => __( 'Datum hinzufügen', 'compassion' ),
    'id'        => $prefix . 'date_agenda',
    'type'      => 'text_date',
) );

}

  • That's a very expensive query, have you considered just using the post_date instead and using pre_get_posts to make sure posts in the future still get shown? It'd be 100x of times faster in most situations – Tom J Nowell Oct 4 '17 at 13:44
  • Also, strtotime returns a Unix timestamp, so this issue shouldn't apply, can we see the code that saves this? – Tom J Nowell Oct 4 '17 at 13:46
  • Hi @TomJNowell, the post date isn't the same as the event date, that's why i can't use. below here's the code that save the metabox (used with CMB2) – olivier Oct 5 '17 at 11:33
  • Is there a specific reason for that? It'd simplify everything and significantly increase the scalability and performance/speed of the query. if you're dealing with sites that get 100 or more concurrent visitors it could mean the difference between a fast site and a database server falling over – Tom J Nowell Oct 5 '17 at 12:11

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.