1

My site's default archive sorting is set to alphabetical, which I am happy with. However, I want to create a custom page template so I can also have an archive page which sorts all posts by date.

I'm using a custom theme, which makes this more complicated than I expected. The standard WP help tutorials don't seem to apply here, or at least I can't figure out where.

This is where I'm at. I've copied the theme's standard archive page and given it a custom template name. However, beyond that I can't figure out where I would edit to force it to sort by date.

Can anyone help?

<?php
/**
 * Template Name: Archive (Sorted by Newest)
 * The template for a custom archive page.
 */
get_header(); ?>

<?php the_archive_title('<h1>','</h1>'); ?>

<section class="content">
     
    <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
                
        <article <?php post_class('grid_block index box'); ?>>
            <?php if( get_theme_mod( 'crissy_post_link', 'block_link' ) == ('block_link')) { ?>
                
                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> 
                    <?php if( get_theme_mod( 'crissy_feat_img' ) == ('rectangle_no_margins')) { get_template_part('feat_img'); } ?>
                    <div class="wrapper">
                        <?php 
                            if( get_theme_mod( 'crissy_feat_img', 'circle' ) != ('rectangle_no_margins')) { get_template_part('feat_img'); }
                            get_template_part('content', get_post_format()); // content depending on post format
                            get_template_part('article_icon'); // article icon at the bottom
                         ?>
                    </div>
               </a> 
               
            <?php } else { ?>
  
                <?php if( get_theme_mod( 'crissy_feat_img' ) == ('rectangle_no_margins')) { get_template_part('feat_img'); } ?>
                <div class="wrapper">
                    <?php 
                        if( get_theme_mod( 'crissy_feat_img' ) != ('rectangle_no_margins')) { get_template_part('feat_img'); }
                        get_template_part('content', get_post_format()); // content depending on post format
                        get_template_part('article_icon'); // article icon at the bottom
                     ?>
                </div>
            <?php } ?>            
        </article>
        
    <?php endwhile; endif; ?>

</section>
<?php 
    get_template_part( 'posts_nav' );   
    get_footer(); 

?>
1

I've taken the answer from here, where they are changing the order in a different way, but it should work for you. It uses the pre_get_posts hook to change the ordering for the archive page only.

add_action( 'pre_get_posts', 'my_change_sort_order');

function my_change_sort_order($query){
    if(is_archive()):
     //If you wanted it for the archive of a custom post type use: is_post_type_archive( $post_type )
       //Set the order ASC or DESC
       $query->set( 'order', 'ASC' ); // ASC = oldest first, DESC = newest first
       //Set the orderby
       $query->set( 'orderby', 'date' );
    endif;
};

Try this at the top of your new page. I haven't tested, so please reply here if any problems or it doesn't work

| improve this answer | |
  • Thanks for the reply. It didn't quite work. I added the code and tested the template. It showed an archive page, however it was not an archive of posts. It was an archive containing only that page. In other words, it was an archive of itself. I hope that makes sense. It seems like something needs to be tweaked to make it show posts. – osarusan Jun 26 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.