How to hide private posts even if user is admin

Question

Even if admins, I dont want to view private posts for custom post type.

Added the following code.

function my_function( $query ) {
    if ( ! is_admin() && ( is_singular( 'my_post_type' ) || is_post_type_archive( 'my_post_type' ) ) ) {
        $query->set( 'post_status', 'publish' );
    }

    // The following is not related to this question
    if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
        $searchable_post_types = get_post_types( array( 'exclude_from_search' => false ) );
        if( in_array( 'my_post_type', $searchable_post_types ) ) {
            unset( $searchable_post_types['my_post_type'] );
            $query->set( 'post_type', $searchable_post_types );
        }
    }
}
add_filter( 'pre_get_posts', 'my_function' );

Is it wrong?
Thanks.

0
, , , pims-tak 2 years 2019-10-30T04:54:29-05:00 0 Answers 84 views 0

Leave an answer

Browse
Browse