How to List Future Posts in WordPress Using Shortcodes

How to List Future Posts in WordPress Using Shortcodes

In WordPress, future posts are those scheduled to be published at a later date. To display a list of these posts on your site, you can create a custom shortcode using the WP_Query class. This method allows you to query posts with a future status and output them wherever you need.

Step-by-Step Guide to Display Future Posts

Follow these steps to create a custom shortcode that lists upcoming posts:

Step 1: Create the Shortcode Function

Add the following code to your theme’s functions.php file or in a custom plugin:

function upcoming_posts_shortcode($atts) {
    $atts = shortcode_atts(array(
        'posts_per_page' => -1,
        'order' => 'ASC',
        'orderby' => 'date',
    ), $atts);

    $args = array(
        'post_status' => 'future',
        'posts_per_page' => $atts['posts_per_page'],
        'order' => $atts['order'],
        'orderby' => $atts['orderby'],
    );

    $future_posts_query = new WP_Query($args);

    ob_start();

    if ($future_posts_query->have_posts()) :
        while ($future_posts_query->have_posts()) :
            $future_posts_query->the_post();
            // Display your future post content here, e.g., title, content, etc.
            echo '<h2>' . get_the_title() . '</h2>';
            echo '<div>' . get_the_content() . '</div>';
        endwhile;
        wp_reset_postdata();
    else :
        echo '<p>No future posts found.</p>';
    endif;

    return ob_get_clean();
}
add_shortcode('upcoming_posts', 'upcoming_posts_shortcode');

Step 2: Use the Shortcode

Once you’ve added the code, you can use the [upcoming_posts] shortcode in your WordPress posts, pages, or widgets to display the list of future posts. Here’s an example:

[upcoming_posts]

This will display all future posts ordered by their scheduled date in ascending order. You can also customize the shortcode by using the following attributes:

  • posts_per_page: Set how many upcoming posts to display. Use -1 to display all upcoming posts. Default is -1.
  • order: Choose whether to display posts in ascending (ASC) or descending (DESC) order. Default is ASC.
  • orderby: Determine how posts are sorted. The default is date (scheduled date).

Example Usage with Custom Attributes

If you want to display only 5 future posts in descending order, use the following shortcode:

[upcoming_posts posts_per_page="5" order="DESC" orderby="date"]

Place the shortcode inside the content editor of your post or page, and WordPress will output the future posts according to your specifications.

Best Practices for Using Future Post Shortcodes

When using this shortcode on your website, consider these best practices:

  • Limit the number of posts displayed to avoid overwhelming your visitors.
  • Customize the display style with CSS to match your site’s design.
  • Use caching to speed up the display of future posts, especially if you have many scheduled posts.

Related Resources

For more information, here are some useful external links:

By following this guide, you’ll be able to easily list and display all upcoming posts on your WordPress website, enhancing user engagement and visibility for scheduled content.

Categories

Related Blogs

Delete Product Image With Product Delete In WooCommerce

In WooCommerce, when you delete a product, by default, the product images are not automatically deleted from the server to avoid accidental data loss. However, you can add a custom action to delete the product images when a product is deleted. Here’s a step-by-step guide to achieve this:

How To Limit Search To Post Titles?

I added a text field to the posts filter form and I use s= parameter to make the search work. But how to search only in the title of the post (not in the content)? how to limit search to post titles? How to search only by Post Title?

Request A Quote