How to Display Published Posts Between Two Dates in WordPress

WordPress provides several ways to filter and display posts, including by date. Whether you’re running a blog or managing a content-heavy site, displaying posts published within a specific date range can be crucial for content curation, analytics, or thematic collections. This tutorial will guide you through the steps to display published posts between two dates in WordPress using both the WordPress admin panel and PHP code.

1. Using the WordPress Admin Panel

Step-by-Step Guide

  1. Log in to Your WordPress Dashboard

    • Access your site’s admin area by navigating to yourdomain.com/wp-admin and logging in with your credentials.
  2. Navigate to the Posts Section

    • From the left-hand menu, click on “Posts”. This will display all your blog posts in a list format.
  3. Use the Date Filter

    • At the top of the posts list, there’s a dropdown labeled “All dates”. Click on this dropdown to select the starting month of your date range.
    • To further refine the selection, you can use the “Filter by date” and “Show All Dates” dropdown options available in the “Screen Options” or “Quick Edit” sections.
  4. Apply the Filter

    • After selecting the desired date, click the “Filter” button. This will display only the posts published within the selected month.
  5. Custom Date Range (Optional)

    • If you need a more specific range (e.g., January 15 to February 15), you may need to use additional filtering options or plugins, as the default admin interface does not support custom date ranges natively.

2. Using PHP Code to Published Posts Between Two Dates in WordPress

For more control or to include this functionality directly in your theme or a custom plugin, you can use PHP code. This method is ideal for developers or advanced users comfortable with editing theme files.

Step-by-Step Guide

  1. Access Your Theme’s Functions File

    • Go to “Appearance” > “Theme File Editor” in your WordPress dashboard, and open the functions.php file of your active theme.
  2. Insert the PHP Code

    • Add the following code to functions.php or a custom plugin to query posts between two dates:
PHP
function display_posts_between_dates($start_date, $end_date) {
$args = array(
'post_type' => 'post',
'date_query' => array(
array(
'after' => $start_date,
'before' => $end_date,
'inclusive' => true,
),
),
);

$query = new WP_Query($args);

if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
the_excerpt();
}
wp_reset_postdata();
} else {
echo 'No posts found between ' . $start_date . ' and ' . $end_date;
}
}

Display Posts on a Page

  • You can call this function in any template file where you want to display the posts. For example, in a custom page template:
PHP
<?php
/* Template Name: Posts Between Dates */
get_header(); 
?>

<div class="content">
    <?php display_posts_between_dates('2024-01-01', '2024-06-30'); ?>
</div>

<?php get_footer(); ?>

Save this file in your theme directory, and then create a new page in WordPress using this template.

3. Using Shortcodes for Flexibility

If you prefer not to edit theme files directly, creating a shortcode is a flexible solution. This allows you to display posts within a date range using a simple shortcode in your posts or pages.

Step-by-Step Guide

Add Shortcode Functionality

  • Add this code to your functions.php file or a custom plugin:
PHP
function posts_between_dates_shortcode($atts) {
    $atts = shortcode_atts(
        array(
            'start_date' => '',
            'end_date'   => '',
        ),
        $atts,
        'posts_between_dates'
    );

    ob_start();
    display_posts_between_dates($atts['start_date'], $atts['end_date']);
    return ob_get_clean();
}
add_shortcode('posts_between_dates', 'posts_between_dates_shortcode');

Use the Shortcode

  • Add the shortcode [posts_between_dates start_date="2024-01-01" end_date="2024-06-30"] in any post or page where you want to display the filtered posts.

Conclusion

Displaying posts between specific dates in WordPress can be easily achieved through the admin panel for basic needs or by using PHP for more advanced customization. Whether you choose the built-in filtering options, custom code, or shortcodes, you have a variety of tools to tailor the display of your content to fit your needs. This flexibility allows you to showcase your posts effectively, keeping your audience engaged with timely and relevant content.

Leave a comment


The reCAPTCHA verification period has expired. Please reload the page.