I got the following code in my functions.php. A couple of months ago it worked (or at least looks like it was), but now it isn't and I don't know what happened.

// ***************************
// ********   Pre get posts para eventos
add_action( 'pre_get_posts', 'custom_post_type_archive_eventos' );

function custom_post_type_archive_eventos( $query ) {

if( $query->is_main_query() && !is_admin() && is_post_type_archive( 'evento' ) ) {

        $today = current_time('Ymd');

        $query->set( 'posts_per_page', '-1' );
        $query->set( 'meta_key', 'fecha_del_evento' );
        $query->set( 'orderby', 'meta_value' );
        $query->set( 'order', 'ASC' );
        $query->set( 'suppress_filters', 'true' );
        $query->set( 'meta_query', array(
                  'key' => 'fecha_del_evento',
                  'compare' => '>=',
                  'value' => '$today'

Now the code doesn't work and events dated in November doesn't work. The URL is http://www.wildlionrecords.com/en/tours/ and I don't know what happened.

Please note that I'm using the following code to display the date, but it sorted as Ymd in database.

                    $datetime = DateTime::createFromFormat('Ymd', get_field('fecha_del_evento'));
                    $fecha_dia = $datetime->format('d');
                    $fecha_mes = $datetime->format('m');
                    $fecha_year = $datetime->format('Y');
                <?php echo $fecha_dia; ?>/<?php echo $fecha_mes; ?>/<?php echo $fecha_year; ?>
  • Your code never worked. The quotes here-- 'value' => '$today'-- would have prevented that. – s_ha_dum Jan 2 '16 at 17:32
  • Yes @Alexey sorry for that. – Kevin Mamaqi Jan 13 '16 at 18:45

PHP handles all content inside single quotes as string therefore your code does not work as expected.

You should remove quotes from

$today in meta_query

and from

true in suppress_filters

| improve this answer | |
  • 1
    Please explain what your suggestions do and why you think they will solve the problem. – s_ha_dum Jan 2 '16 at 17:30
  • @s_ha_dum the code there is written wrong and this makes it actually to work. PHP handles all content inside single quotes as a string and not as actual value represented by the variable $today. – Ján Bočínec Jan 2 '16 at 23:38
  • I know what this does, but many here have very little understanding of the code. You should explain your solution not just post the code. Please edit the explanation into the answer. Comments should not contain any critical information. – s_ha_dum Jan 2 '16 at 23:42
  • s_ha_dum I've added the explanation. Thanks for comment. I'll take it in account in future. – Alexey Jan 3 '16 at 20:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.