Support

Account

Home Forums General Issues Difficulties to sort a post by DatePicker

Solved

Difficulties to sort a post by DatePicker

  • Hello !

    I want to order my posts by a date field that I have created with ACF but it doesn’t work correctly because the loop doesn’t end. I mean I have 3 posts that I want to be ordered, they will be displayed 4 times (4 times because I want 12 posts per page)

    I already have looked the documentations available and so this is what I tried :

    <?php $posts = get_posts(array(
      'post_type'     => 'post',
      'posts_per_page'  => -1,
      'meta_key'      => 'start_date',
      'orderby'     => 'meta_value_num',
      'order'       => 'DESC'
    ));
    
    if( $posts ): ?> 
      <?php foreach( $posts as $post ):  
        setup_postdata( $post )
        ?>
    
    <?php $now = time();
          $date_one_timestamp = strtotime(get_field('end_date', false, false));
        if ($now < $date_one_timestamp ) { ?> 
    	<div class="event-total-page">
    	<div class="date-time">
    	<div class="date-custom-page">   
    
              <?php $dateformatstring = '<p>j</p> <p>F Y</p>'; ?>
              <?php $unixtimestamp = strtotime(get_field('start_date', false, false));
              echo date_i18n($dateformatstring, $unixtimestamp); ?>
      	  <a href="<?php the_permalink(); ?>"><p class="view-event btn-primary">BEKIJK EVENT</p>
            </div>
            </div>
    
    <?php } else {
    //do nothing
    }
    ?>
    <?php endforeach; ?>
    
      <?php wp_reset_postdata(); ?>
    <?php endif; ?>

    To explain a little my code : I have a condition telling that every events that have their end date older that now, disappear.
    And secondly I use $unixtimestamp to display the date in my language.

    I don’t understand why the loop doesn’t work correctly, because the setup_postdata( $post ) ?

    Thank you for your help.

  • First, are you sure your getting something in the query?

    
    // see if your getting any results
    // put this after get_posts();
    echo '<pre>'; print_r($posts); echo '</pre>';
    

    Second question, if you are getting posts, where is this code called? Is it in a template field or a file that is loaded using one of the WP functions like get_header(), get_footer(), get_template_part(), etc?

    If not then try adding

    
    // add this before you reference $post
    global $post;
    
  • Hello John,

    Thanks a lot for your reply !

    Indeed, when I tried again my code this morning there were any results, yesterday I had a loop but I probably have changed something.
    So I tried what you told for the first solution and it gives me the array of each posts with their ID, post_author, post_date, post_date_gmt, post_content …
    I can show you a screenshot if it’s needed.

    And for your second question I have created a page templates/custom page which groups all my events (and that’s why I want them to be ordered).

    Thank you for your time and if you have any idea of ​​what can cause an error in my code I would be happy !

  • Did you try adding

    
    global $post;
    

    at the beginning of your code?

  • Yes I tried but it didn’t work, nothing appears.
    The
    echo '<pre>'; print_r($posts); echo '</pre>';
    display me the array with all data and for the
    global $post;
    there’s nothing that appears.
    To be sure I show you where I put it in my code :

    <?php global $post; ?>
    
    <?php $posts = get_posts(array(
      'post_type'     => 'post',
      'posts_per_page'  => -1,
      'meta_key'      => 'start_date',
      'orderby'     => 'meta_value_num',
      'order'       => 'DESC'
    ));
    
    if( $posts ): ?> 
      <?php foreach( $posts as $post ):  
        setup_postdata( $post )
        ?>
    
      <?php $now = time();
        $date_one_timestamp = strtotime(get_field('end_date', false, false));
        if ($now < $date_one_timestamp ) { ?>        
    
    <div class="event-total-page">
          <div class="category-name-event"></div>
          <div class="date-time">
            <div class="date-custom-page">   
    
              <?php $dateformatstring = '<p>j</p> <p>F Y</p>'; ?>
              <?php $unixtimestamp = strtotime(get_field('start_date', false, false));
    
              echo date_i18n($dateformatstring, $unixtimestamp); ?>
              <a href="<?php the_permalink(); ?>"><p class="view-event btn-primary">BEKIJK EVENT</p></a>
            </div>
          </div>
    

    Even if I try it in another place it doesn’t work.

    Thanks a lot John for your time.

  • 
    // what value is being returned from acf
    echo get_field('end_date', false, false);
    
    //What is this producing?
    $date_one_timestamp = strtotime(get_field('end_date', false, false));
    echo $date_one_timestamp;
    
    // and what happens here
    echo (date('Y-m-d H:i:s', $date_one_timestamp));
    
  • Hi John,
    Thanks for your help again !

    The value returned for the
    echo get_field('end_date', false, false);
    is the end date that I fill in the backend and with this code it gives me the date at this format : 2016-10-21 15:00:00 (for the first event), 2017-07-15 17:30:00 (for the second event)…

    For the second one :

    $date_one_timestamp = strtotime(get_field('end_date', false, false));
    echo $date_one_timestamp;

    It gives some weird numbers like 1477062000 or 1500139800

    And the third one :
    echo (date('Y-m-d H:i:s', $date_one_timestamp));
    It gives me exactly the same than the first one, it’s the value of the end_date field like : 2016-12-23 22:00:00

    The 1970-01-01 00:00:00 is the value by default if the field is not filled.

    I’ll continue to search what’s wrong in my code.
    Thanks a lot John !

  • Helping is why I’m here.

    The reason I wanted you to do those things was to see if you are getting a valid date from ACF and that the date is being converted to the correct time using strtotime. Basically, the first and second dates should always be the same, just the time should be added to the second one. If they are different then there is a problem.

  • Hello John !

    I finally find a solution and I post it here if it can help someone :

    <?php $custom_query_args = array('numberposts'  => -1,
      'posts_per_page' => 10,
      'post_type'   => 'post',
      'meta_query' => array(
          array(
              'key' => 'type',
              'value' => 'evenement',
              'compare' => '='
          )
      ),
        'meta_key' => 'start_date',
        'orderby' => 'meta_value',
        'order' => 'ASC',
    );
    
    // Instantiate custom query
    $custom_query = new WP_Query( $custom_query_args );
    
    // Output custom query loop
    if ( $custom_query->have_posts() ) :
        while ( $custom_query->have_posts() ) :
            $custom_query->the_post(); ?>
    
    // Here is your code
    
    //Close
    <?php
        endwhile;
    endif;
    wp_reset_postdata(); ?>

    I use WP_Query and resolve my problem of the double meta_key by using the meta_query to display both my posts who are type “event” and to order event by my “start_date” field.

    Thank you !

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.