I need a little help here with ordering my archive page. I made a custom WordPress archive page for my content-type “activiteiten” and called it: archive-activiteiten.php. And used the action “pre_get_posts” to make the query’s.
My archive template has two query’s in it:
- The first one to display events in the future
- The second one to display events in the past
I would like to change the order of the first query. So that it displays the first upcoming event on top.
The archive-activiteiten.php code:
<?php
/**
* The template for displaying Archive pages.
*
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* @package Simon van der Aa
* @since Simon van der Aa 1.0
*/
get_header(); ?>
<div class="container main">
<div class="row">
<div class="span8">
<section class="komende-activiteiten">
<h1 class="page-title">Activiteiten</h1>
<?php
while ( have_posts() ) : the_post(); $eventdate = DateTime::createFromFormat('Ymd', get_field('activiteit_datum')); if($eventdate->format('Ymd') >= date('Ymd')) : ?>
<article class="agenda-item">
<h2><a href="<?php echo get_permalink() ?>" title="<?php echo get_the_title() ?>"><?php echo get_the_title() ?></a></h2>
<?php if ( has_post_thumbnail() ) :?>
<a href="<?php echo get_permalink(); ?>" title="<?php echo get_the_title(); ?>">
<figure class="article-image">
<?php the_post_thumbnail('besturen-thumb', array('class' => 'img-rounded')); ?>
<?php the_post_thumbnail_caption(); ?>
</figure>
</a>
<?php endif; if ( get_field('activiteit_datum', '') ) : $date = DateTime::createFromFormat('Ymd', get_field('activiteit_datum')); ?>
<div class="activiteit-datum">
<span class="dag"><?php echo $date->format('d');?></span>
<span class="maand"><?php echo $date->format('F');?></span>
</div>
<?php endif; echo the_excerpt(); if ( get_field('activiteit_inschrijfformulier', '') ) :?><a href="<?php echo get_permalink(); ?>#inschrijven" class="btn">Inschrijven voor de activiteit</a><?php endif; ?>
</article>
<?php endif; endwhile; ?>
</section><!-- komende-activiteiten -->
<section class="geweest-activiteiten">
<h2>Geweest</h2>
<table class="table table-striped agenda-archief">
<thead>
<tr>
<th>Datum</th>
<th>Activiteit</th>
</tr>
</thead>
<tbody>
<?php while ( have_posts() ) : the_post(); $eventdate = DateTime::createFromFormat('Ymd', get_field('activiteit_datum')); if($eventdate->format('Ymd') < date('Ymd')) : ?>
<tr>
<td><?php echo $eventdate->format('d-m-Y') ?></td>
<td><a href="<?php echo get_permalink(); ?>" title="<?php echo get_the_title(); ?>"><?php echo get_the_title(); ?></a></td>
</tr>
<?php endif; endwhile;?>
</tbody>
</table>
</section><!-- geweest-activiteiten -->
<?php simonvanderaa_content_nav( 'nav-below' ); ?>
</div><!--/ span8 -->
<div class="span4">
<?php get_sidebar(); ?>
</div><!--/ span4 -->
</div><!--/ row -->
</div><!--/ container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
And in my functions.php I use the following pre_get_posts action
function my_post_queries( $query ) {
// do not alter the query on wp-admin pages and only alter it if it's the main query
if (!is_admin() && $query->is_main_query()){
// alter the query for the home and category pages
if(is_home()){
$query->set('posts_per_page', 3);
}
if ( is_post_type_archive('commissie') ){
$query->query_vars['posts_per_page'] = 5;
$query->query_vars['order'] = 'desc';
return;
}
if ( is_post_type_archive('nieuws') ){
$query->query_vars['posts_per_page'] = 10;
$query->query_vars['order'] = 'desc';
return;
}
if ( is_post_type_archive('besturen') ){
$query->query_vars['posts_per_page'] = 1;
$query->query_vars['order'] = 'desc';
return;
}
if ( is_post_type_archive('faqs') ){
$query->query_vars['posts_per_page'] = 99;
$query->query_vars['order'] = 'asc';
return;
}
if ( is_post_type_archive('activiteiten') ){
$query->query_vars['posts_per_page'] = 10;
$query->query_vars['orderby'] = 'meta_value';
$query->query_vars['meta_key'] = 'activiteit_datum';
return;
}
}
}
add_action( 'pre_get_posts', 'my_post_queries' );
The first thing you need to do for your post type is to add a meta query to the argument. I’m not sure how your date is stored, but I’ll assume it’s in seconds since Epoch (‘U’).
The above only shows post in the future and obviously you can use the opposite to show events in the past.
So to keep this to one query you now need something to indicate whether you’re going to want the future or past events. Using something like a query arg in the URL is probably the best way to do it:
So now http://www.yoursite.co.uk/activiteiten would show posts in the future and http://www.yoursite.co.uk/activiteiten/?past=true would show posts in the past.
Hope that helps you with your solution.