To list down future posts in WordPress

To list down future posts in WordPress, you can use the `WP_Query` class to customize the query and fetch future posts. The future posts are the ones with a post status of “future,” and their publication date is scheduled in the future.

To create a custom shortcode called `[upcoming_posts]` that lists down future posts in WordPress, follow these steps:

**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**

Now you can use the `[upcoming_posts]` shortcode in your WordPress posts, pages, or widgets to display the list of future posts.

For example:

[upcoming_posts]

By default, the shortcode will display all future posts ordered by their scheduled date in ascending order. However, you can also customize the shortcode attributes as follows:

– `posts_per_page`: Number of upcoming posts to display. Set to `-1` to display all upcoming posts. Default is `-1`.
– `order`: Post order. Use `’ASC’` for ascending order or `’DESC’` for descending order. Default is `’ASC’`.
– `orderby`: Sort upcoming posts by a specific field. Use `’date’` for scheduled date. Default is `’date’`.

Example usage with custom attributes:

 

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

Remember to place the shortcode within the content editor in WordPress to display the future posts on the frontend of your website.

 

Categories

Related Blogs

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?

WordPress Shortcode

WordPress Shortcodes are a powerful feature that allows you to add dynamic content and functionality to your WordPress posts, pages, and widgets. Shortcodes are essentially small snippets of code enclosed in square brackets, like `[shortcode]`, that can be placed within the content area of your WordPress site.

How To Add Menu In WordPress Admin Panel

How to add menu in WordPress admin panel – In this post we see how we can add custom menu to admin sidebar. Sometimes when we on WordPress and we need to show some features or any information in admin page then we can use this code snippet to create the same.