0

I am using something like this to query and display future or scheduled posts on my homepage:

<?php query_posts('post_status=future&posts_per_page=10&order=ASC'); while ( have_posts() ) : the_post(); ?>

//doing stuff

<?php endwhile; ?>
<?php wp_reset_query(); ?>

This is working with great success. Although, these scheduled or future posts don't want to show up in a search or tag search.

I would like to get the future posts searchable and the tags contained within them clickable. They (the tags) are currently clickable but throw an error on click. Takes me to the dreaded "Sorry No posts matched your criteria".

So, how to enable WP search for and to include future posts and tags from future posts?

2 Answers 2

1

To achieve the intended result you will have to modify the query by adding the future value to the post_status parameter via the pre_get_posts filter hook.

add_action( 'pre_get_posts', 'se338152_future_post_tag_and_search' );
function se338152_future_post_tag_and_search( $query )
{
    // apply changes only for search and tag archive
    if ( ! ( $query->is_main_query() && (is_tag() || is_search()) ) )
        return;

    $status = $query->get('post_status');
    if ( empty($status) )
        $status = ['publish'];
    if ( !is_array($status) )
        $status = (array)$status;
    $status[] = 'future';

    $query->set('post_status', $status);
}

Conditional tags is_search() and is_tax() will allow you to modify the query only on the search page or the tag archive.

1
  • Thank you for the detailed answer.This worked perfectly. Commented May 18, 2019 at 14:09
0

Future and scheduled posts aren't considered published so you'd need to customize the queries in those templates to include those results.

You'd need to customize the search.php template in your theme. Add a new WP Query similar to what you've done in your example that pulls future and scheduled posts into the template. Then replace the data in that template with the data from your own query.

For the tags you'd need to create a taxonomy template in your theme and do something very similar as with the search template.

1
  • You should include some code samples. I've tried the method you mentioned for search, it doesn't work. In fact, the search function is just pulling up all posts. Like a normal home.php would in a blog. Commented May 17, 2019 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.