Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Sorts Posts by Specific Category for Archives WordPress

  • SOLVED

I have an archives template setup for both an Upcoming Events page, and a Blog page, in which both pages are an archive for posts with category Blog checked, and category Event checked.

This is the blog archive page: http://lifebridgecypress.org/blog

And here is the Upcoming Events page: http://lifebridgecypress.org/events

I like the way that the Events page is setup, but <strong>I do not want to show past events... only ALL future events, up until midnight of the day of the event.</strong> In other words, I want to show ALL future events, and to have them disappear from the Upcoming Events page once midnight of the next day comes.

Here's the code that I have for the Events page...

<?php if (have_posts()) : ?>
<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
<?php /* If this is a category archive */ if (is_category()) { ?>
<h2 class="pagetitle">Upcoming Events</h2>
<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
<h2 class="pagetitle">Archive for <?php the_time('F jS, Y'); ?></h2>
<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
<h2 class="pagetitle">Archive for <?php the_time('F, Y'); ?></h2>
<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
<h2 class="pagetitle">Archive for <?php the_time('Y'); ?></h2>
<?php /* If this is a search */ } elseif (is_search()) { ?>
<h2 class="pagetitle">Search Results</h2>
<?php /* If this is an author archive */ } elseif (is_author()) { ?>
<h2 class="pagetitle">Author Archive</h2>
<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
<h2 class="pagetitle">Blog Archives</h2>
<?php } ?>

<?php
$cat = get_query_var('cat');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//The Query
query_posts('showposts=100&orderby=meta_value&meta_key=event_date&order=ASC&cat=' .$cat. '&paged='.$paged);
//The Loop
$count = 0;
while ( have_posts() ) : the_post(); $count++;
//The Title
$title = $post->post_title;
?>
<?php
$event_locationn = get_post_meta($post->ID, 'event_address', true);
$event_venuee = get_post_meta($post->ID, 'event_venuee', true);
$event_person = get_post_meta($post->ID, 'event_person', true);
$event_phone = get_post_meta($post->ID, 'event_phone', true);
$event_datee = get_post_meta($post->ID, 'event_date', true);

$removews = array(" " => "+");
$event_locationn_plus = strtr($event_locationn, $removews);
?>
<dl>
<dt><a title="<?php echo $title; ?>" href="<?php the_permalink(); ?>"><?php echo $title; ?></a></dt>
<div class="datebox">
<div class="postdate">
<div class="month m-<?php $dtes = get('event_date'); $timestamp = strtotime($dtes); echo date('n', $timestamp); //month ?>"></div>
<div class="day d-<?php $dtes = get('event_date'); $timestamp = strtotime($dtes); echo date('j', $timestamp); //day ?>"></div>
<div class="year y-<?php $dtes = get('event_date'); $timestamp = strtotime($dtes); echo date('Y', $timestamp); //year ?>"></div>
</div></div><!--end postdate-->
<dd>
<ul>
<li>Venue: <?php echo $event_venuee; ?></li>
<li>Location: <?php echo $event_locationn; ?></li>
<li><?php if (!empty($event_person)) { ?>Contact Person: <?php echo $event_person; ?></li><?php } ?>
<li><?php if (!empty($event_phone)) { ?>Phone Number: <?php echo $event_phone; ?></li><?php } ?>
</ul>
<?php the_excerpt(); ?>
</dd>
</dl>
<?php endwhile; ?>
</div>


For the Blog archive page, I'd like it to be setup just like the Events archive page, except I DO want to show all blog posts that have been created and posted in the past. I also want it to look the same way as it does when you search for a specific word on the site, like "Nepal".

Like this search result: http://lifebridgecypress.org/index.php?s=nepal

The code for that is within the search.php file, which is pasted below...

<?php if (have_posts()) : ?>
<h2 class="pagetitle">Search Results</h2>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('&laquo; Previous Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Next Entries &raquo;') ?></div>
</div><!--end navigation-->

<?php while (have_posts()) : the_post(); ?>

<div class="post"><!-- start post -->
<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>
<div class="postdate">
<div class="month m-<?php the_time('m') ?>"><?php the_time('M') ?></div>
<div class="day d-<?php the_time('d') ?>"><?php the_time('d') ?></div>
<div class="year y-<?php the_time('Y') ?>"><?php the_time('Y') ?></div>
</div>
<div class="the-excerpt"><?php the_excerpt() ?></div>
<div class="entry-meta">Posted in <?php the_category(', ') ?> <strong>|</strong> <?php edit_post_link('Edit','','<strong>|</strong>'); ?> <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></div>
</div><!-- end post -->

<?php endwhile; ?>



Also, one last tidbit... on the homepage (http://lifebridgecypress.org), I already have an Upcoming Events column to show the next 2 upcoming events, sorted by next upcoming event on top, and then the following event on the bottom. Events disappear once midnight of the day following the event hits. If you need to use part of this code for the solution to my Events archive page dilemma, here's the code for that...

<?php
//The Query
query_posts(array(
'posts_per_page' => 2,
'meta_key' => 'event_date',
'meta_value' => date('Y-m-d', strtotime('-1 day')),
'meta_compare' => '>',
'orderby' => 'meta_value',
'order' => 'ASC'
));

//The Loop
$count = 0;
while ( have_posts() ) : the_post(); $count++;
//The Title
$title = $post->post_title;
$event_datee = get_post_meta($post->ID, 'event_date', true);
?>
<dl>
<dt><a title="<?php echo $title; ?>" href="<?php the_permalink(); ?>"><?php echo $title; ?></a></dt>
<div class="postdate">
<div class="month m-<?php $dtes = get('event_date'); $timestamp = strtotime($dtes); echo date('n', $timestamp); //month ?>"></div>
<div class="day d-<?php $dtes = get('event_date'); $timestamp = strtotime($dtes); echo date('d', $timestamp); //day ?>"></div>
<div class="year y-<?php $dtes = get('event_date'); $timestamp = strtotime($dtes); echo date('Y', $timestamp); //year ?>"></div>
</div>
<dd><?php the_excerpt(); ?></dd>
</dl>
<?php endwhile; ?>



Thanks for your help!

Answers (1)

2010-12-28

rilwis answers:

Hi Spencer,

For event page, change the query code:

query_posts('showposts=100&orderby=meta_value&meta_key=event_date&order=ASC&cat=' .$cat. '&paged='.$paged);

to:

query_posts(array(
'posts_per_page' => 100,
'meta_key' => 'event_date',
'meta_value' => date('Y-m-d'),
'meta_compare' => '>',
'orderby' => 'meta_value',
'order' => 'ASC',
'cat' => $cat,
'paged' => $paged
));


I see that you use custom page template for Blog page, so just replace old code in this template with this one:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'posts_per_page' => 100,
'order' => 'DESC',
'paged' => $paged
));

if (have_posts()):
?>
<h2 class="pagetitle">Blog Archives</h2>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('&laquo; Previous Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Next Entries &raquo;') ?></div>
</div><!--end navigation-->

<?php while (have_posts()) : the_post(); ?>
<div class="post"><!-- start post -->
<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>
<div class="postdate">
<div class="month m-<?php the_time('m') ?>"><?php the_time('M') ?></div>
<div class="day d-<?php the_time('d') ?>"><?php the_time('d') ?></div>
<div class="year y-<?php the_time('Y') ?>"><?php the_time('Y') ?></div>
</div>
<div class="the-excerpt"><?php the_excerpt() ?></div>
<div class="entry-meta">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit','','|'); ?> <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></div>
</div><!-- end post -->

<?php endwhile; ?>


Spencer Barfuss comments:

<strong>The events code you posted works great!</strong> However, I'm getting an error after pasting the blog code you posted, and it's saying that it's on the last line of code in that file, which is the <?php get_footer(); ?>.

Here's the code that I copied and pasted from what you pasted above...

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'posts_per_page' => 100,
'order' => 'DESC',
'paged' => $paged
));
if (have_posts()):
?>

<h2 class="pagetitle">Blog Archives</h2>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('&laquo; Previous Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Next Entries &raquo;') ?></div>
</div><!--end navigation-->

<?php while (have_posts()) : the_post(); ?>

<div class="post"><!-- start post -->

<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>

<div class="postdate">
<div class="month m-<?php the_time('m') ?>"><?php the_time('M') ?></div>
<div class="day d-<?php the_time('d') ?>"><?php the_time('d') ?></div>
<div class="year y-<?php the_time('Y') ?>"><?php the_time('Y') ?></div>
</div>

<div class="the-excerpt"><?php the_excerpt() ?></div>
<div class="entry-meta">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit','','|'); ?> <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></div>

</div><!-- end post -->
<?php endwhile; ?>

</div><!-- end main div -->
</div><!-- end whitebox div -->
<?php get_footer(); ?>


rilwis comments:

I forgot to close "if" statement. Try this code:

[[LINK href="http://pastebin.com/ZxxL3Gch"]]http://pastebin.com/ZxxL3Gch[[/LINK]]


Spencer Barfuss comments:

One last question, how can I exclude the Events from showing up on the Blog archives page? It's currently showing both...