admin管理员组文章数量:1122832
For a specific website I'm working with, the desired behaviour is to be able to return search results for ALL posts including the appropriate search phrase - however posts which are marked as private are currently not being returned in the search results unless the user is logged in.
Is it possible to modify the search so that the results of pages marked private are included in the search?
(I am able to make modifications to the [child] theme - which already includes logic to modify the search results - but unless I'm missing something it appears that there is further filtering happening outside the child theme filtering)
For a specific website I'm working with, the desired behaviour is to be able to return search results for ALL posts including the appropriate search phrase - however posts which are marked as private are currently not being returned in the search results unless the user is logged in.
Is it possible to modify the search so that the results of pages marked private are included in the search?
(I am able to make modifications to the [child] theme - which already includes logic to modify the search results - but unless I'm missing something it appears that there is further filtering happening outside the child theme filtering)
Share Improve this question asked Sep 25, 2024 at 4:40 davidgodavidgo 3353 silver badges14 bronze badges1 Answer
Reset to default 1If you want to show Private Posts to non-logged in user then you can try the given code. Also showing Private posts to Non-Logged In User not recommended.
With this approach the private content will expose to all users, which may not be recommended for most cases.
<?php
function include_private_posts_in_search( $query ) {
if ( $query->is_search && ! is_admin() ) {
/**
* Here we have added 'publish' and 'private' post status, we can add more as per the need.
*/
$query->set( 'post_status', array( 'publish', 'private' ) );
}
return $query;
}
add_filter( 'pre_get_posts', 'include_private_posts_in_search' );
版权声明:本文标题:filters - Is it possible to modify a WP search query to return results for private pages when not logged in? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736288734a1928156.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论