admin管理员组文章数量:1426594
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 badges2 Answers
Reset to default 1To 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.
本文标签: loopInclude future posts in tags and in search
版权声明:本文标题:loop - Include future posts in tags and in search 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745483040a2660259.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论