How to Filter Posts By Year on WordPress?

14 minutes read

Filtering posts by year on WordPress can be achieved by utilizing the built-in functionality of the WordPress CMS. This feature allows you to sort and display posts based on their publish year in a customized manner. Here is a step-by-step guide on how to filter posts by year on WordPress:

  1. Login to your WordPress admin panel.
  2. Navigate to the Appearance section and click on "Widgets."
  3. Locate the "Archives" widget and drag it to your desired widget area on the right-hand side.
  4. Expand the widget settings by clicking on the small arrow on the top-right corner of the widget block.
  5. Configure the options for the "Archives" widget. Under the "Display as" section, choose the "Dropdown" or "List" option, based on your preference.
  6. Tick the checkbox labeled "Show Post Counts" to display the number of posts published in each year alongside the year itself.
  7. Check the "Yearly" option under the "Show" section to enable the filtering of posts by year.
  8. Save the changes by clicking on the "Save" or "Close" button, depending on your WordPress version.
  9. Visit your website and locate the newly added "Archives" widget in your chosen widget area.
  10. Click on the "Year" dropdown (if you selected the dropdown option) or view the list of years (if you chose the list option) to filter the posts accordingly.
  11. Click on a specific year to display all the posts published in that particular year.


By following these steps, you can effectively filter and display posts based on their publish year on your WordPress website, providing a convenient way for visitors to browse through your content based on timeframes.

Best WordPress Books of April 2024

1
WordPress: The Missing Manual: The Book That Should Have Been in the Box

Rating is 5 out of 5

WordPress: The Missing Manual: The Book That Should Have Been in the Box

2
WordPress All-in-One For Dummies (For Dummies (Computer/Tech))

Rating is 4.9 out of 5

WordPress All-in-One For Dummies (For Dummies (Computer/Tech))

3
WordPress Explained: Your Step-by-Step Guide to WordPress (2020 Edition)

Rating is 4.7 out of 5

WordPress Explained: Your Step-by-Step Guide to WordPress (2020 Edition)

4
Professional WordPress: Design and Development

Rating is 4.5 out of 5

Professional WordPress: Design and Development

5
WordPress Plugin Development Cookbook: Create powerful plugins to extend the world's most popular CMS, 2nd Edition

Rating is 4.4 out of 5

WordPress Plugin Development Cookbook: Create powerful plugins to extend the world's most popular CMS, 2nd Edition

6
WordPress 5 Complete: Build beautiful and feature-rich websites from scratch, 7th Edition

Rating is 4.3 out of 5

WordPress 5 Complete: Build beautiful and feature-rich websites from scratch, 7th Edition

7
Building Web Apps with WordPress: WordPress as an Application Framework

Rating is 4.2 out of 5

Building Web Apps with WordPress: WordPress as an Application Framework

8
WordPress for Beginners 2020: A Visual Step-by-Step Guide to Mastering WordPress (Webmaster Series)

Rating is 4 out of 5

WordPress for Beginners 2020: A Visual Step-by-Step Guide to Mastering WordPress (Webmaster Series)


How to highlight the filtered posts by year on WordPress?

To highlight filtered posts by year on WordPress, you can follow these steps:

  1. Open your WordPress dashboard and navigate to Appearance > Customize. This will open the WordPress Customizer.
  2. Look for the "Additional CSS" option in the Customizer menu and click on it. This is where you can add custom CSS code to style your website.
  3. In the "Additional CSS" section, add the following CSS code to style the filtered posts:
1
2
3
4
5
/* Highlight filtered posts by year */
.archive .post {
  border: 1px solid #ccc; /* Add a border to each post */
  margin-bottom: 20px; /* Add some spacing between posts */
}


This CSS code adds a border to each post and some spacing between them.

  1. Click on the "Publish" button to save your changes.


Now, when users filter and view posts by year, the filtered posts will be highlighted with a border and spacing.


How to reset the post filter by year on WordPress?

To reset the post filter by year on WordPress, you can follow these steps:

  1. Log in to your WordPress admin dashboard.
  2. Go to the "Appearance" tab on the left-hand side menu and select "Widgets."
  3. Look for the "Archives" widget and find the year filter you want to reset.
  4. Expand the year filter section by clicking the down arrow next to it.
  5. In the expanded section, you should see a "Remove" or "Delete" link. Click on it to remove the year filter.
  6. Once you have removed the year filter, click on the "Save" button to save the changes.
  7. Visit your WordPress website to check if the post filter by year has been reset.


Note: The steps above may vary slightly depending on the WordPress theme or version you are using.


How to organize posts by year on a WordPress website?

To organize posts by year on a WordPress website, follow these steps:

  1. Log in to your WordPress admin dashboard.
  2. Go to the "Appearance" tab on the left-hand side and click on "Widgets".
  3. Look for the "Archives" widget and drag it to the desired widget area on your website (e.g., sidebar or footer).
  4. In the "Archives" widget settings, select the "Yearly" option.
  5. Customize the widget title if desired.
  6. Save the widget settings.
  7. Visit your website to see the changes. You should now have a list of years displayed, allowing users to click on each year to see posts published in that specific year.


Alternatively, if you prefer a more visually appealing way to display posts by year, you can opt for a WordPress plugin. Here's how to do it using the "Yearly Archive" plugin:

  1. Install and activate the "Yearly Archive" plugin from the WordPress repository.
  2. Go to your WordPress admin dashboard and navigate to "Appearance" > "Widgets".
  3. Drag and drop the "Yearly Archive" widget into your desired widget area.
  4. Customize the widget settings, such as the title, display format, and other options.
  5. Save the widget settings.
  6. View your website, and you should now have an organized archive displayed as a dropdown menu, allowing users to select any specific year to browse posts for that year.


Remember to adjust the styling of the widgets to match your website's design for a seamless user experience.

Best WordPress Hosting Providers in 2024

1
Vultr

Rating is 5 out of 5

Vultr

  • Ultra-fast Intel Core
  • High Performance and Cheap Cloud Dedicated Servers
  • 1 click install Wordpress
  • Low Price and High Quality
2
Digital Ocean

Rating is 4.9 out of 5

Digital Ocean

  • Active Digital Community
  • Simple Control Panel
  • Starting from 5$ per month


What is the plugin recommendation for filtering posts by year on WordPress?

One popular plugin for filtering posts by year on WordPress is "Smart Archives Reloaded." It provides several options for displaying archives, including filtering by year, month, or day. This plugin also offers customization options, allowing you to style and format the archive display to match your website's design.


What is the default sorting order for posts on WordPress?

The default sorting order for posts on WordPress is by the date in descending order, which means the newest posts are displayed first.


How to create a custom filter for posts on WordPress by year?

To create a custom filter for posts on WordPress by year, you can follow these steps:

  1. Open your theme's functions.php file. You can find this file in your theme's folder.
  2. Add the following code to define a custom filter dropdown in the WordPress admin interface:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function custom_year_filter()
{
    global $wp_query;
    $year = isset($_GET['year']) ? $_GET['year'] : '';

    // Get all years in which posts are published
    $years = get_posts(array(
        'posts_per_page' => -1,
        'fields' => 'ids',
        'post_type' => 'post',
        'post_status' => 'publish',
        'orderby' => 'post_date',
        'order' => 'DESC',
        'date_query' => array(
            array(
                'column' => 'post_date_gmt',
                'after' => '1 year ago',
            ),
        ),
    ));

    // Generate the dropdown HTML
    echo '<select name="year">';
    echo '<option value="">Select Year</option>';
    foreach ($years as $year_item) {
        $year_selected = ($year == get_the_date('Y', $year_item)) ? 'selected' : '';
        echo '<option value="' . get_the_date('Y', $year_item) . '" ' . $year_selected . '>' . get_the_date('Y', $year_item) . '</option>';
    }
    echo '</select>';
}

function custom_year_filter_posts($query)
{
    global $pagenow;
    if (is_admin() && $pagenow == 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] == 'post' && isset($_GET['year']) && $_GET['year'] != '') {
        $query->query_vars['date_query'] = array(
            array(
                'year' => $_GET['year'],
            ),
        );
    }
}

add_action('restrict_manage_posts', 'custom_year_filter');
add_action('pre_get_posts', 'custom_year_filter_posts');


  1. Save the functions.php file and refresh your WordPress admin interface.
  2. You should now see a custom dropdown selectbox labeled "Select Year" above the posts list.
  3. Selecting a year from the dropdown and clicking the "Filter" button will filter the posts list based on the selected year.


Note: This code assumes you are filtering the built-in "post" post type and that you want to filter by the year of the post's publication date. You can modify the arguments in the get_posts function to filter a different post type or use a different date field to filter.


How to enable the post filter by year for specific post types in WordPress?

To enable the post filter by year for specific post types in WordPress, you need to follow these steps:

  1. Open your functions.php file in your WordPress theme folder. This file controls the functions and features of your theme.
  2. Add the following code to register a custom taxonomy named "post_year" for your desired post type(s). Replace "post_type" with the name of your post type.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
function custom_post_year_taxonomy() {
    register_taxonomy(
        'post_year',
        'post_type',
        array(
            'label' => __( 'Year' ),
            'rewrite' => array( 'slug' => 'post-year' ),
            'hierarchical' => true,
        )
    );
}
add_action( 'init', 'custom_post_year_taxonomy', 0 );


  1. Save the changes to your functions.php file.
  2. Navigate to your WordPress admin dashboard and find the new "Year" taxonomy under your desired post type(s) in the sidebar.
  3. Click on "Year" and add the relevant year(s) for each post.
  4. Now, you need to customize your post archive template to filter posts by year. Open your archive.php or taxonomy.php file depending on your theme's structure.
  5. In the archive.php or taxonomy.php file, add the following code before the main loop:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$archive_year = get_query_var( 'post_year' );
$args = array(
    'post_type' => 'post_type',
    'tax_query' => array(
        array(
            'taxonomy' => 'post_year',
            'field'    => 'slug',
            'terms'    => $archive_year,
        ),
    ),
);
$query = new WP_Query( $args );


Replace "post_type" with the name of your post type.

  1. Replace your existing loop code with the following:
1
2
3
4
5
6
7
8
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // Your post template code here
    }
} else {
    // No posts found template code here
}


  1. Save the changes to your archive.php or taxonomy.php file.
  2. Now, you should be able to filter your posts by year for your specific post type(s) by visiting the archive or taxonomy pages with the "post_year" parameter in the URL. For example, https://yourwebsite.com/post-year/2022/.
Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To select posts from a specific taxonomy in WordPress, you can use the WP_Query class to create a custom query that retrieves posts based on the taxonomy term. You would first need to get the term ID of the taxonomy you want to query, and then use that term ID...
To get WordPress posts count using PHP cURL, you can follow these steps:Initialize a cURL session using the curl_init() function. $ch = curl_init(); Set the URL to the WordPress website&#39;s REST API endpoint that provides access to the posts data. $url = &#3...
There are times when you need to disable comments on your WordPress site. There are two cases contextual to disabling WordPress comments: Disabling comments for individual posts and pages Disabling comments completely Disabling comments for individual WordPr...