Display Scheduled WordPress Posts with a Custom Shortcode

Code Snippets PHP PHP Snippets Wordpress WordPress Development WordPress Functions WordPress How-To WordPress Tips

Display Scheduled WordPress Posts with a Custom Shortcode Tutorial/Guide

WordPress allows you to schedule posts for future publication. You can showcase these upcoming posts by creating a simple shortcode using WP_Query. This method lets you list future-dated posts dynamically across your site.

How to Create a Shortcode for Future Posts

Use this step-by-step guide to build the shortcode:

Step 1: Write the Function

Add this function to your theme's functions.php file or a site-specific 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'],
    );

    $query = new WP_Query($args);
    ob_start();

    if ($query->have_posts()) :
        while ($query->have_posts()) :
            $query->the_post();
            echo '<h4>' . get_the_title() . '</h4>';
            echo '<div>' . get_the_content() . '</div>';
        endwhile;
        wp_reset_postdata();
    else :
        echo '<p>No upcoming posts found.</p>';
    endif;

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

Step 2: Insert the Shortcode

You can now use [upcoming_posts] in any post or page. Here's the basic shortcode:

[upcoming_posts]

Customize it with optional attributes:

  • posts_per_page: How many future posts to show.
  • order: Display order (ASC or DESC).
  • orderby: Sort by field (e.g., date).

Sample with Attributes

[upcoming_posts posts_per_page="3" order="ASC" orderby="date"]

Best Practices

  • Apply styling via CSS for a polished look.
  • Limit the output to keep it clean.
  • Enable cache plugins to optimize speed.

Further Reading

This feature is great for promoting scheduled content like announcements, events, or upcoming posts.

💡 Have a Coding Problem?

Search our archives or reach out to our team for solutions and expert advice.