最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

loop - Include future posts in tags and in search

programmeradmin1浏览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?

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.

发布评论

评论列表(0)

  1. 暂无评论