• As you may know, all of the typical solutions for displaying upcoming events in WordPress are awful, so I’m accomplishing it with simple custom fields. For each event the site admin will enter a Numeric date “YYYYMMDD” in the back end.

    What I need to find out is how to code in PHP for the script to first get php:the_date <‘YYYYMMDD’> and then have WordPress filter events dynamically, only displaying posts that have a value greater than or equal to the numeric value printed (which would be the YYYYMMDD format of “today” which is pulled in via PHP:the_date…

Viewing 2 replies - 1 through 2 (of 2 total)
  • <?php
    //display all posts with custom field event_start_date >= to current date
    $event_date = date('Ymd');
    $args=array(
      'meta_value'=> $event_date,
      'meta_key'=>'event_start_date',
      'meta_compare'=> '>=',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts with event_start_date >= ' . $event_date;
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Trying to do exactly the same thing, but I want to use a custom post type (“events”) and order by the value of the custom field.

    I can get it to order by the value of the custom field, but not filter out posts with a “past” date.

    Here’s the code for mine (I know it’s incomplete, but I don’t know what to do):

    <?php $event_date = the_date('Ymd'); ?>
    
    <?php $loop = new WP_Query( array(
        'post_type' => 'events',
        'posts_per_page' => 10,
        'meta_key'=>'Date-yyyymmdd',
        'meta_compare'=>'>=',
        'orderby'=>'meta_value',
        'order'=>ASC ) ); ?>
    
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Create a Dynamic Filter Showing Posts of Numeric Value’ is closed to new replies.