문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 wordpress.stackexchange
scroll top