0

I'm trying to generate a table for a custom post type events, in which I have a few custom fields, one of these is the date. I want to display the posts in the table and order them by the Date value. This is my code:

Date format: 07/07/2015 (DAY/MONTH/YEAR)

Right now, the loop isn't working and I don't know how to order by the date.

<div class="container">
  <div class="row">

        <table>
          <tr>
            <th>Artista</th>
            <th>Fecha</th>
            <th>Ciudad</th>
            <th>Información</th>
          </tr>
        <?php $i = 0;?>
        <?php while ( have_posts() ) : the_post(); ?>
            <?php if($i == 0){ echo "<tr>"; }?>
                <td><?php the_title(); ?></td>
                <td><?php the_field('fecha_del_evento'); ?></td>
                <td><?php the_field('lugar_del_evento'); ?></td>
                <td><?php the_content(); ?></td>
            <?php $i++;?>
            <?php if($i == 4){ echo "</tr>"; $i=0;}?>
        <?php endwhile; ?>
            <?php if($i < 4) echo '</tr>'; ?>
        </table>

        <?php else:  ?>
          <div class="page-header"><h1>Uh Oh!</h1></div>
          <p>Sorry, for some reason the contents of this page isn't displaying.</p>
        <?php endif; ?>


  </div><!-- END OF ROW -->
</div><!-- END OF CONTAINER -->
  • In what format is your date in your custom field – Pieter Goosen Jul 7 '15 at 7:02
  • @PieterGoosen It is 07/07/2015 (DAY/MONTH/YEAR) – Kevin Mamaqi Jul 7 '15 at 7:05
  • You are not going to sort by date with that format unfortunately. Your dates in custom fields must be stored as unix timestamp or YYYY-mm-dd – Pieter Goosen Jul 7 '15 at 7:08
  • @PieterGoosen Ok, I can change how the date is stored. [link] (i.imgur.com/yj5UMjG.png) – Kevin Mamaqi Jul 7 '15 at 7:27
  • Note, you need to have the correct format stored in db, otherwise it will not work – Pieter Goosen Jul 7 '15 at 7:30
0

I found a solution which displays the posts ordered by the date value, and also removes the past ones.

<div class="container">
    <div class="row">  

     <table>
      <tr>
        <th>Artista</th>
        <th>Fecha</th>
        <th>Ciudad</th>
        <th>Información</th>
      </tr>

      <?php
      $today = current_time('Ymd');

      $args = array(
        'post_type' => 'evento',
        'post_status' => 'publish',
        'posts_per_page' => '-1',
        'meta_query' => array(
          array(
            'key' => 'fecha_del_evento',
            'compare' => '>=',
            'value' => $today,
            )
          ),
        'meta_key' => 'fecha_del_evento',
        'orderby' => 'meta_value',
        'order' => 'ASC',
        );

      $query = new WP_Query($args);
      if ($query->have_posts()) :
        while ($query->have_posts()) : $query->the_post(); 
      ?>
      <?php $i = 0;?>
      <?php if($i == 0){ echo "<tr>"; }?>
      <td><?php the_title(); ?></td>
      <?php $i++;?>
      <td><?php the_field('fecha_del_evento'); ?></td>
      <?php $i++;?>
      <td><?php the_field('lugar_del_evento'); ?></td>
      <?php $i++;?>
      <td><?php echo get_the_content(); ?></td>
      <?php $i++;?>
      <?php if($i == 4){ echo "</tr>"; $i=0;}?>
    <?php endwhile; ?>
    <?php if($i < 4) echo '</tr>'; ?>
  </table>

  <?php wp_reset_postdata(); ?>

<?php endif; ?>
</div><!-- END OF ROW -->
</div><!-- END OF CONTAINER -->

Notice that this code (inside the proper query, of course) would be enough to order the posts by date:

'meta_key' => 'fecha_del_evento',
            'orderby' => 'meta_value',
            'order' => 'ASC',
            );
  • Note, running custom queries in place of the main query is expensive and unnecessary. – Pieter Goosen Jul 13 '15 at 9:03
  • Hello @PieterGoosen thanks for your answer, but I'm not a coder and don't know how to implement a normal query to solve this case. I used this because is what I found. In addition, you mention "expensive" regarding performance, right? – Kevin Mamaqi Jul 13 '15 at 9:07
  • Yes, that is correct, on which template is this. From your question I took that this is not a page template. If it is a page template, yes, then my approach will not work, then you will have to use yours, there are no other way of doing it. – Pieter Goosen Jul 13 '15 at 9:11
0

I have a similar setup as to what you were going for. This block of code goes in the template file:

    <?php 

// get posts
$posts = get_posts(array(
    'meta_key'  => 'date',
    'orderby'   => 'meta_value_num',
    'order'     => 'ASC'
));


// loop
if( $posts ) {

    foreach( $posts as $post ) {

        setup_postdata( $post );

        // do something

    }

    wp_reset_postdata();

}

?>                                  



<?php if ( have_posts() ) : ?>
    <table>
        <tbody>
            <tr class="schedule-header">
                <th scope="col" class="header-cell schedule-cell">Date</th>
                <th scope="col" class="header-cell schedule-cell">Time</th>
                <th scope="col" class="header-cell schedule-cell">Venue</th>
                <th scope="col" class="header-cell schedule-cell">Address</th>
                <th scope="col" class="header-cell schedule-cell">Tix</th>                              
            </tr>
        </tbody>    


<?php endif; ?>


<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <?php get_template_part( 'template-parts/content-front-page'); ?>
    <?php endwhile; else: ?>

        <h5>Sorry! Either I have been too lazy to put in my show dates or no one is booking me because they're mad at me. Are you mad at me?</h5>

<?php endif; ?>

    </table 

This block of code goes in the functions.php:

function my_pre_get_posts( $query ) {

// do not modify queries in the admin
if( is_admin() ) {

    return $query;

}


// only modify queries for 'event' post type
if( isset($query->query_vars['post']) && $query->query_vars['post_type'] == 'post' ) {

    $query->set('orderby', 'meta_value');   
    $query->set('meta_key', 'start_date');   
    $query->set('order', 'DESC'); 

}


// return
return $query;

}

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.