0

i’m using a wp-query to display posts from a category on a custom post type page. it works fine, and I’ve been also abble to sort my posts depending of datepicker field called “date_debut”. my posts are events posts, so i’m using a start date field (“date_debut”) and a end date field (“date_fin”).

here is my code :

<?php 

// args
$today = date("Ymd");

    $args = array (
     'category' => 1,
            'posts_per_page' => -1,
            'meta_key'       => 'date_debut',
            'orderby'       => 'meta_value_num',
            'order'          => 'ASC',
);


// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

/* MY POST CONTENT */

    <?php endwhile; ?>

<?php endif; ?>

<?php wp_reset_query(); ?>

now I’m trying to display only posts that are in the future, or today. but it’s not working, and I can’t find out why…

here is my code :

<?php 

// args
$today = date("Ymd");

    $args = array (
     'category' => 1,
            'posts_per_page' => -1,
            'meta_key'       => 'date_debut',
            'orderby'       => 'meta_value_num',
            'order'          => 'ASC',
   'meta_query' => array(
        array(
            'key'       => 'date_debut',
            'compare'   => '<=',
            'value'     => $today,
        ),
         array(
            'key'       => 'date_fin',
            'compare'   => '>=',
            'value'     => $today,
        )
    ),
);


// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

/* MY POST CONTENT */

    <?php endwhile; ?>

<?php endif; ?>

<?php wp_reset_query(); ?>

“date_debut” and “date_fin” format is the default ACF format : yymmdd

I don’t understand what I’m doing wrong, when using the second code, nothing is displayed, my query is empty.

I tried to change date format, both for th today date and for the custom fields but without success…

can anybody help me with this ?

thanks a lot,

  • the Y in php's date is 4 digits- 2014. Is the date saved in meta also 4 digits, or just 2? – Milo Jun 21 '14 at 18:12
  • let me check with print_r – user2882154 Jun 21 '14 at 18:15
  • I don't know how to find out the format of my dates... when print_r('date_debut'), i get date debut and not the date.. what can I do to know this ? @Milo – user2882154 Jun 21 '14 at 18:17
  • try the_field('date_debut'); – Milo Jun 21 '14 at 19:01

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.