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.

 

Leave a Comment

Your email address will not be published. Required fields are marked *