WordPress.org

Ready to get started?Download WordPress

Forums

Create a Dynamic Filter Showing Posts of Numeric Value (3 posts)

  1. brianfeister
    Member
    Posted 4 years ago #

    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...

  2. MichaelH
    Member
    Posted 4 years ago #

    <?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().
    ?>
  3. logangreer
    Member
    Posted 4 years ago #

    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(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic