0

I want to show future posts in search results, categories, etc.

This site says to use the following code:

$args = array(
'post_status' => array('publish', 'future'),
);
query_posts( $args );

But where do I put it, and does it replace anything? Here's the relevant section of search.php:

<?php if (have_posts()) : ?>        
<h1 id="archive-title"><?php printf( __('Search Results - %s', 'perkins'), get_search_query()); ?></h1>
<?php                                           
while (have_posts()) : the_post();                  
get_template_part('content', get_post_format());
endwhile;
else :                  
get_template_part('content', 'none');
endif;
?>
2

That site you linked to is completely wrong. You should never ever use query_posts to run any type of query. Take your time and read the following post on this matter:

You also would not want to run any type of custom query in place of the main query, this raises/causes more issues than what it actually solves. You can also read the following post on this:

You should be using pre_get_posts to alter the main query to your needs. This is the correct way to alter what is being returned by the main query

add_action( 'pre_get_posts', function ( $q ) {
    if (    !is_admin() 
         && $q->is_main_query() 
    ) {
        $q->set( 'post_status', ['publish', 'future'] );
    }
});

Just remember, the code above effects all pages on the front end. This will even show future posts on single post pages as well which normally 404. You should adjust this to suite your specific needs

3
  • Useful info, thanks. But I added this to functions and it broke the site (competely blank).
    – lkl
    Apr 3 '16 at 11:46
  • lkl, "completely blank" is not helpful. When you get a blank page it's best to turn on WP_DEBUG or watch your error logs for an actual error message.
    – kovshenin
    Apr 3 '16 at 12:23
  • 1
    @lkl There was a missing &&. It should work now. Apr 3 '16 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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