• Resolved cluesew

    (@cluesew)


    I need to create standard posts in a specific category named “Calendar”
    Within that post I have a custom field named “event_date” where I input the date in this format: 09/01/2014

    I want the page to display all posts in the category “Calendar” into 2 div’s like this:

    <div id=”upcoming”>
    <h1>Upcoming Events</h1
    (the post excerpt)
    </div>

    <div id=”past”>
    <h1>Past Events</h1
    (the post excerpt)
    </div>

    So far, I am able to sort the category posts’ results by the custom field like so:

    <?php query_posts ( array(
    	'category_name' => 'calendar',
    	'orderby' => 'meta_value',
    	'meta_key' => 'event_date',
    	'order' => 'DSC'
    	) );
    
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php get_template_part( 'entry' ); ?>
    <?php endwhile; endif; ?>

    But I need to now split them into the “upcoming” and “past” divs.

    Utilizing the following code as a feeble attempt to do so only outputs “no events found.”

    <?php query_posts ( array(
    	'category_name' => 'calendar',
    	'orderby' => 'meta_value',
    	'meta_key' => 'event_date',
    	'order' => 'DSC'
    	) );
    
    $events_found = false;
    
        if ( have_posts() ) {
            while ( have_posts() ) {
                the_post();
                $event_date = get_post_meta($post->ID, 'event_date', true);
    
                switch ($daterange) {
                    case "current":
                        if ($event_date >= time() ) {
    			echo the_content();
                            $events_found = true;
                        }
                        break;
    
                    case "past":
                        if ($event_date < time() ) {
                         	echo the_content();
                            $events_found = true;
                        }
                        break;
                }
            }
        }
    
        wp_reset_query();
    
        if (!$events_found) {
            echo "<p>no events found.</p>";
        }
    ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • You cannot sort dates properly in the format ‘mm/dd/yyyy’. You must either store them in the format ‘yyyy/mm/dd’, or use a special sql query to create a sort key with the rearranged date.

    Here is an article that shows one way to do the sort: http://wordpress.mcdspot.com/2014/09/01/sort-custom-field-date-mmddyyyy-format/

    Thread Starter cluesew

    (@cluesew)

    Thank you for this excellent article vtzyzzy. Since this is a new site, it’s not a problem to input the date in this format: yyyy/mm/dd (or any format for that matter).
    But to then split the results into UPCOMING & PAST divs by that same meta key is beyond my capabilities.

    It seems logical to me to have 2 loops and use a statement in the loop similar as this:
    if ($event_date >= time() )
    however I think I need to add some code to the functions.php that somehow formats the custom meta key to the $unixtime format.

    I’ve tried but no results show up.

    I’ve been borrowing code from this article but, again, it is beyond my capabilities. All of the custom meta keys from this article are available when creating standard post and I can get them to show up utilizing his shortcode, but the dates for the event_date meta key always come out to be January 01, 1970.

    I think this will work:

    $in_div = false;
    $today = date('Y/m/d');
    if ( have_posts() ) {
       while ( have_posts() ) {
          the_post();
          $event_dt = $post->sort_key;
          if ( $event_dt > $today) {
             if ( $in_div == false ) {
                echo "<div id='upcoming'>\n";
                echo "<h1>Upcoming Events</h1>\n";
                $in_div = 'upcoming';
             }
          } else {
             if ( $in_div == 'upcoming' ) {
                echo "</div>\n";
             }
             if ( $in_div != 'past' ) {
                echo "<div id='past'>\n";
                echo "<h1>Past Events</h1>\n";
                $in_div = 'past';
             }
          }
          // Use your own code for the display
          the_title();
          $eventdate = get_post_meta($post->ID, 'eventdate', true);
          echo " Event Date: $eventdate SORT KEY:$post->sort_key<br />";
       }
       // Close any open div
       if ( $in_div ) {
          echo "</div>\n";
       }
    }
    Thread Starter cluesew

    (@cluesew)

    Thank you. I implemented your code, changed the post_meta name to the correct one of “event_date” and the output on the page is this:

    Past Events
    Wig Out! Get Your Wig On!’s 9th Year Anniversary Show Event Date: 2014/11/10 SORT KEY:
    New Site Launch! Event Date: 2014/08/25 SORT KEY:
    Calendar test post Event Date: 2014/10/01 SORT KEY:

    It doesn’t seem to be grabbing the data from the meta key “event_date”

    I changed 2nd line of code to this but no difference $today = date('yyyy/mm/dd');

    Did you add the filter functions to your functions.php?

    If so, did you set the $mam_global_xxx variables?

    The code I gave above was just to substitute for lines 19-27 in the second set of PHP in the article I referenced. That is, using the code from the article, replace this:

    if ( have_posts() ) {
       while ( have_posts() ) {
          the_post();
          // Use your own code for the display
          the_title();
          $eventdate = get_post_meta($post->ID, 'eventdate', true);
          echo " Event Date: $eventdate SORT KEY:$post->sort_key<br />";
       }
    }

    with this:

    $in_div = false;
    $today = date('Y/m/d');
    if ( have_posts() ) {
       while ( have_posts() ) {
          the_post();
          $event_dt = $post->sort_key;
          if ( $event_dt > $today) {
             if ( $in_div == false ) {
                echo "<div id='upcoming'>\n";
                echo "<h1>Upcoming Events</h1>\n";
                $in_div = 'upcoming';
             }
          } else {
             if ( $in_div == 'upcoming' ) {
                echo "</div>\n";
             }
             if ( $in_div != 'past' ) {
                echo "<div id='past'>\n";
                echo "<h1>Past Events</h1>\n";
                $in_div = 'past';
             }
          }
          // Use your own code for the display
          the_title();
          $eventdate = get_post_meta($post->ID, 'eventdate', true);
          echo " Event Date: $eventdate SORT KEY:$post->sort_key<br />";
       }
       // Close any open div
       if ( $in_div ) {
          echo "</div>\n";
       }
    }

    Sorry, bit of brain fog. If you put your dates in the yyyy/mm/dd format, you don’t need all the filter functions and global variables. You can just use this:

    <?php query_posts ( array(
    	'category_name' => 'calendar',
    	'orderby' => 'meta_value',
    	'meta_key' => 'event_date',
    	'order' => 'DESC'
    	) );
    $in_div = false;
    $today = date('Y/m/d');
    if ( have_posts() ) {
       while ( have_posts() ) {
          the_post();
          $event_dt = get_post_meta($post->ID, 'event_date', true);
          if ( $event_dt > $today) {
             if ( $in_div == false ) {
                echo "<div id='upcoming'>\n";
                echo "<h1>Upcoming Events</h1>\n";
                $in_div = 'upcoming';
             }
          } else {
             if ( $in_div == 'upcoming' ) {
                echo "</div>\n";
             }
             if ( $in_div != 'past' ) {
                echo "<div id='past'>\n";
                echo "<h1>Past Events</h1>\n";
                $in_div = 'past';
             }
          }
          // Use your own code for the display
          the_title();
          echo " Event Date: $event_dt<br />";
       }
       // Close any open div
       if ( $in_div ) {
          echo "</div>\n";
       }
    }
    Thread Starter cluesew

    (@cluesew)

    Thank you so much, vtxyzzy. This works great! I’ll keep the filter functions as it’s easier to enter the date traditionally. Cheers!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Sort loop by meta value, then split results into 2 divs’ is closed to new replies.