Forums

[resolved] Ordering a table using a custom field date. (3 posts)

  1. webmatt
    Member
    Posted 1 year ago #

    Hello, recently through this post here in support, I created a table listing future events on my blog using this code in a page theme:

    <?php
    $args=array(
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 20,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {  ?>
     	<TABLE border="1" summary="Future Events">
      <CAPTION><EM>Future Events </EM></CAPTION>
      <TR><TH>Event<TH>Author<TH>Where<TH>Date
      <?php
      while ($my_query->have_posts()) : $my_query->the_post();
        $where = get_post_meta($post->ID, 'where', true);
        $date = get_post_meta($post->ID, 'date', true); ?>
        <tr><th><?php the_title(); ?><td><?php the_author(); ?><td><?php echo $where; ?><td><?php echo $date; ?>
      <?php endwhile; ?>
      </TABLE>
    <?php }
    wp_reset_query();
    ?>

    Only I would like to list only the future events, not the past. Is there any way to filter the custom field (date) to display only the events of the day ahead in the table and order the table based in custom field date, and not by post id?

    The custom field uses the same date format for the blog (DD/MM/YYYY)

    Thanks for any help!

  2. vtxyzzy
    Member
    Posted 1 year ago #

    Dates cannot be easily compared when in DD/MM/YYYY format.

    If you can change the format of the custom field to YYYY-MM-DD, you can do the selection directly in the arguments to the query:

    $args=array(
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 20,
      'caller_get_posts'=> 1,
      'meta_key' = 'date',
      'meta_value' = date('Y-m-d'),
      'meta_compare' = '>'
    );
  3. webmatt
    Member
    Posted 1 year ago #

    Hello vtxyzzy.

    Thanks for the reply.

    In fact yesterday I created a custom field called "ISODATA"to put the date in the format in which you quoted. That is, it displays the value in "dd / mm / yyyy", but the date is sorted by the "ISODATA" field.

    But I do not know how to show only the posts after the current date, and got the answer from your post.

    Thanks for your help!

Topic Closed

This topic has been closed to new replies.

About this Topic