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.

 

Related Blogs

Ajax Add to Cart Quantity on Shop WooCommerce

To enable AJAX add to cart with quantity selectors on the WooCommerce shop page, you’ll need to use JavaScript to handle the AJAX request and update the cart quantity dynamically. Below are the steps to achieve this:

WooCommerce: Check if Product Category is in the Cart

To check if a specific product category is in the cart during the `woocommerce_before_cart` action hook, you can use a similar approach as in the previous answer. However, in this case, you need to use the `woocommerce_before_cart_contents` action hook, which fires before the cart items are displayed on the cart page. Here’s how you can achieve this:

Request A Quote

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.