Support

Account

Home Forums General Issues Don't show post when they have passed

Solved

Don't show post when they have passed

  • Hello !

    I have a list of events and I would like to know if it’s possible to hide them when they have passed and how.

    I call my date like that :

    <?php 
    $date = get_field('jour_de_levenement', false, false);
    $date = new DateTime($date);
    ?>
    <p class="date-event"><?php echo $date->format('j'); ?>
    
    <p class="month"><?php echo $date->format('F '); ?><?php echo $date->format('Y'); ?>
    

    It seems like I need to use query_post and I tried this code :

    <?php
    
      $current_date = date('j F Y');
      $current_date = strtotime( $current_date );
    
      query_posts(array('post_type' => 'evenement',
                  'showposts' => 12,
                  'meta_key'=>'jour_de_levenement',  
                  'orderby' => 'meta_value', 
                  'order' => ASC));
    while (have_posts()) : the_post(); 
    
      $post_date = get('jour_de_levenement');
      $post_date = strtotime( $post_date );
    
      if( $post_date > $current_date ):
    
    ?>
     <div class="date-custom-page">   
            <?php 
              $date = get_field('jour_de_levenement', false, false);
              $date = new DateTime($date);
            ?>
            <p class="date-event"><?php echo $date->format('j'); ?>
    
            <p class="month"><?php echo $date->format('F '); ?><?php echo $date->format('Y'); ?>
    
    <?php
      endif;
      endwhile;
    ?>

    But it didn’t hide my old posts. I really appreciate if someone has an idea.

    Thanks a lot.

  • Hi,

    what field type is the jour_de_levenement field? And what value is returned?

  • Hi fabianmarz,

    jour_de_levenement is a Date field and the code :

    <p class="date-event"><?php echo $date->format('j'); ?>
    <p class="month"><?php echo $date->format('F '); ?><?php echo $date->format('Y'); ?>

    Return me the day, month and year like that : 06 September 2016

    I wanted to split the date into different <p> and then to be able to customize the date and it’s working.

    For the value, I use get_field with my field jour_de_levenement.

    Maybe I need to put this (my div date-custom-page) before the query_post ?

    Thanks a lot for your time !

  • Hi @snogy

    I believe you can do it by using this query:

    $args = array('post_type' => 'evenement',
        'showposts' => 12,
        'meta_key' => 'jour_de_levenement',  
        'meta_compare' => '>',  
        'meta_value' => date("Ymd"),
        'orderby' => 'meta_value_num',
        'order' => 'ASC'
    );

    I hope this helps 🙂

  • Thanks a lot James !

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

You must be logged in to reply to this topic.