这应该可以做到,使用pre_get_posts
:
function wpse_275546_hide_distribution_post( $query ) {
/* Don\'t filter posts in admin, or the current post when viewing a single post. */
if ( ! is_admin() && ! ( $query->is_main_query() && is_single() ) ) {
/**
* Get current meta query if it exists, otherwise an empty array.
* This is to modifiy, so that we don\'t override any existing meta queries.
*/
$meta_query = $query->get( \'meta_query\' ) ?: array();
/* Add meta query to find posts that do not have distribitions set to 1. */
$meta_query[] = array(
\'key\' => \'distribution\',
\'value\' => \'1\',
\'compare\' => \'!=\',
);
/* Apply meta query. */
$query->set( \'meta_query\', $meta_query );
}
}
add_action( \'pre_get_posts\', \'wpse_275546_hide_distribution_post\' );
有一件事我不确定,那就是你
is_single()
. 是否仍要允许用户查看单个帖子?如果是这样的话,您是否仍然想对任何其他可能的循环(如小部件)隐藏它?
如果您可以在查看单个帖子时到处显示帖子,则可以删除$query->is_main_query() &&
部分我补充道,为了确保主帖子出现在单帖子视图上,但仍然对次要帖子隐藏它们。