Is there a simple way to sort posts via year and then – within each year sort them by title?

[ad_1]

I have many publications (posts) and I want them to be showed chronologically: 2022, 2021, 2020, …. And within each year I want to posts be sorted by title. I use PHP so I want to know if there’s a simple loop (I’m not an expert on backend/PHP) to be created. Base that I’m using looks like this:

​

`<?php`
`$args = array(`
`’post_type’ => ‘publications’,`
`’post_status’ => ‘publish’,`
`’posts_per_page’ => -1,`
                    `);`
`$loop = new WP_Query( $args );`
`while ( $loop->have_posts() ) : $loop->the_post();`
`the_content();`
`endwhile;`
`wp_reset_postdata();`            
`?>`

[ad_2]
1 Comment
  1. In your $args array, add a orderby array.

    $args = array(
    ‘post_type’ => ‘publications’,
    ‘post_status’ => ‘publish’,
    ‘posts_per_page’ => -1,
    ‘orderby’ => array(
    ‘title’ => ‘ASC’,
    ‘date’ => ‘DESC’
    )
    );

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer