Sort loop by meta value, then split results into 2 divs
-
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/2014I 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)
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.