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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

If 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' );

本文标签: filtersIs it possible to modify a WP search query to return results for private pages when not logged in