Split one WordPress posttype archive page in two Querys

admin

Administrator
Staff member
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:

<ol>
<li>The first one to display events in the future</li>
<li>The second one to display events in the past</li>
</ol>

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:

Code:
    &lt;?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(); ?&gt;

&lt;div class="container main"&gt;
        &lt;div class="row"&gt;
            &lt;div class="span8"&gt;
                &lt;section class="komende-activiteiten"&gt;
                    &lt;h1 class="page-title"&gt;Activiteiten&lt;/h1&gt;
                    &lt;?php 
                    while ( have_posts() ) : the_post(); $eventdate = DateTime::createFromFormat('Ymd', get_field('activiteit_datum')); if($eventdate-&gt;format('Ymd') &gt;= date('Ymd')) : ?&gt;
                    &lt;article class="agenda-item"&gt;
                        &lt;h2&gt;&lt;a href="&lt;?php echo get_permalink() ?&gt;" title="&lt;?php echo get_the_title() ?&gt;"&gt;&lt;?php echo get_the_title() ?&gt;&lt;/a&gt;&lt;/h2&gt;
                        &lt;?php if ( has_post_thumbnail() ) :?&gt;
                            &lt;a href="&lt;?php echo get_permalink(); ?&gt;" title="&lt;?php echo get_the_title(); ?&gt;"&gt;
                            &lt;figure class="article-image"&gt;
                            &lt;?php the_post_thumbnail('besturen-thumb', array('class' =&gt; 'img-rounded')); ?&gt;
                            &lt;?php the_post_thumbnail_caption(); ?&gt;
                            &lt;/figure&gt;
                            &lt;/a&gt;
                        &lt;?php endif; if ( get_field('activiteit_datum', '') ) : $date = DateTime::createFromFormat('Ymd', get_field('activiteit_datum')); ?&gt;
                            &lt;div class="activiteit-datum"&gt;
                                &lt;span class="dag"&gt;&lt;?php echo $date-&gt;format('d');?&gt;&lt;/span&gt;
                                &lt;span class="maand"&gt;&lt;?php echo $date-&gt;format('F');?&gt;&lt;/span&gt;
                            &lt;/div&gt;
                        &lt;?php endif; echo the_excerpt(); if ( get_field('activiteit_inschrijfformulier', '') ) :?&gt;&lt;a href="&lt;?php echo get_permalink(); ?&gt;#inschrijven" class="btn"&gt;Inschrijven voor de activiteit&lt;/a&gt;&lt;?php endif; ?&gt;
                    &lt;/article&gt;
                    &lt;?php endif; endwhile; ?&gt;
                &lt;/section&gt;&lt;!-- komende-activiteiten --&gt;
                &lt;section class="geweest-activiteiten"&gt;
                    &lt;h2&gt;Geweest&lt;/h2&gt;
                    &lt;table class="table table-striped agenda-archief"&gt;
                        &lt;thead&gt;
                            &lt;tr&gt;
                                &lt;th&gt;Datum&lt;/th&gt;
                                &lt;th&gt;Activiteit&lt;/th&gt;
                             &lt;/tr&gt;
                        &lt;/thead&gt;
                        &lt;tbody&gt;           
                    &lt;?php while ( have_posts() ) : the_post(); $eventdate = DateTime::createFromFormat('Ymd', get_field('activiteit_datum')); if($eventdate-&gt;format('Ymd') &lt; date('Ymd')) : ?&gt;
                        &lt;tr&gt;
                          &lt;td&gt;&lt;?php echo $eventdate-&gt;format('d-m-Y') ?&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;a href="&lt;?php echo get_permalink(); ?&gt;" title="&lt;?php echo get_the_title(); ?&gt;"&gt;&lt;?php echo get_the_title(); ?&gt;&lt;/a&gt;&lt;/td&gt;
                        &lt;/tr&gt;
                        &lt;?php endif; endwhile;?&gt;
                        &lt;/tbody&gt;
                    &lt;/table&gt;
                &lt;/section&gt;&lt;!-- geweest-activiteiten --&gt;
                &lt;?php simonvanderaa_content_nav( 'nav-below' ); ?&gt;
            &lt;/div&gt;&lt;!--/ span8 --&gt;
            &lt;div class="span4"&gt;
                &lt;?php get_sidebar(); ?&gt;
            &lt;/div&gt;&lt;!--/ span4 --&gt;
        &lt;/div&gt;&lt;!--/ row --&gt;
    &lt;/div&gt;&lt;!--/ container --&gt;

&lt;?php get_sidebar(); ?&gt;
&lt;?php get_footer(); ?&gt;

And in my functions.php I use the following pre_get_posts action

Code:
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() &amp;&amp; $query-&gt;is_main_query()){

    // alter the query for the home and category pages 

    if(is_home()){
      $query-&gt;set('posts_per_page', 3);
    }

   if ( is_post_type_archive('commissie') ){
           $query-&gt;query_vars['posts_per_page'] = 5;
           $query-&gt;query_vars['order'] = 'desc';
           return;
       }
   if ( is_post_type_archive('nieuws') ){
           $query-&gt;query_vars['posts_per_page'] = 10;
           $query-&gt;query_vars['order'] = 'desc';
           return;
       }
   if ( is_post_type_archive('besturen') ){
           $query-&gt;query_vars['posts_per_page'] = 1;
           $query-&gt;query_vars['order'] = 'desc';
           return;
       }
   if ( is_post_type_archive('faqs') ){
           $query-&gt;query_vars['posts_per_page'] = 99;
           $query-&gt;query_vars['order'] = 'asc';
           return;
       }

   if ( is_post_type_archive('activiteiten') ){
           $query-&gt;query_vars['posts_per_page'] = 10;
           $query-&gt;query_vars['orderby'] = 'meta_value';
           $query-&gt;query_vars['meta_key'] = 'activiteit_datum';
           return;
       }
  }
}
add_action( 'pre_get_posts', 'my_post_queries' );