How to list articles by year based on url?

Hie all, I am trying to list posts by year based on the provided url (for example: wordpressite.com/2004 - this should list all articles created in 2004), unfortunately all suggested methods from previous solutions solve the problem if the year to be queried is given.

I tried creating the year.php file suggested in the wordpress hierarchy as well as date.php, but the_loop() just gives everything, not listing articles from the specific year.

?php
    query_posts('posts_per_page=10');

    if ( have_posts() ) : while ( have_posts() ) : the_post();
        get_template_part( 'archive-content', get_post_format() );
    endwhile; endif;

    wp_reset_query();
?

And this is the individual article I would like to have

div class="row" style="margin-top: 15px;"
  a href="?php the_permalink() ?"
    div class="col col-sm-3 remove-left-margin"
        ?php $url = wp_get_attachment_url( get_post_thumbnail_id(get_the_ID()), 'thumbnail' ); ?
        img src="?php echo $url ?" class="archive-card-image" /
    /div
    div class="col col-sm-9"
        divp class="archive-card-date"?php the_date(); ?/p/div
        ?php
            $categories = get_the_category();
            if ( ! empty( $categories ) ) {
                echo 'div class="card-article-tag"a class="archive-card-topic" href="' . esc_url( get_category_link( $categories[0]-term_id ) ) . '"' . esc_html( $categories[0]-name ) . '/a/div';
            }
        ?
        divh4 class="archive-card-title"b?php the_title(); ?/b/h4/div
    /div
  /a
/div

I am still very new to wordpress, would appreciate any help.

wp_get_archives('type=yearly')

The documentation does not state how I can use my article structure mentioned above.

Topic archive-template wp-get-archives loop theme-development Wordpress

Category Web


Avoid using query_posts()

Please don't use

query_posts('posts_per_page=10');

in your date.php template file as it will override the main query instance and thus affect the main loop. This can truly ruin the day!

If you need to modify e.g. the posts_per_page for the main date query, use the pre_get_posts action to modify the query variables, before the database request is made.

There's a harsh warning on query_posts() usage in the manual.

So remove that part toghether with wp_reset_query() and it should work again!

Yearly Date Archive: Modifying the number of posts

Here's an example how to change the posts per page for the yearly date archive:

add_action( 'pre_get_posts', function( $query ) {
    if ( is_admin() || ! $query->is_main_query() || ! $query->is_year() ) {
        return;
    }
    $query->set( 'posts_per_page', 10 ); // Adjust the number to your needs!
} );

Here we use the is_year() to target the yearly archive queries and the is_main_query() to target only main queries.

Yearly Date Archive: Supporting the date-year.php template file.

We can also add a support for the date-year.php template file, by modifying the template filter example from the manual:

/**
 * Add a support for date-year.php template files.
 */
add_filter( 'date_template', function( $templates ) {
    if ( ! is_year() ) {
        return $templates;
    }
    if ( ! is_array( $templates ) && ! empty( $templates ) ) { 
        $templates = locate_template( [ "date-year.php", $templates ], false ); 
    } elseif ( empty( $templates ) ) { 
        $templates = locate_template( "date-year.php", false ); 
    } else {
        $new_template = locate_template( [ "date-year.php" ] ); 
        if ( ! empty( $new_template ) ) { 
            array_unshift( $templates, $new_template ); 
        } 
    } 
    return $templates; 
} );

That way we are only working with the yearly date archive and not the others, like month and day if using the date.php template file.

Note

Add the code snippets into your functions.php file in the current theme directory or create a plugin. Never copy/paste some code snippets from the internet directly to a live site. Always test first on dev/local installs.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.