loop - Include future posts in tags and in search

I am using something like this to query and display future or scheduled posts on my homepage:<?php query_posts('

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?

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?

Share Improve this question asked May 17, 2019 at 19:23 ben.kaminskiben.kaminski 1778 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745483040a4629643.html

相关推荐

  • loop - Include future posts in tags and in search

    I am using something like this to query and display future or scheduled posts on my homepage:<?php query_posts('

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信