1

I've been looking for a plugin that works like a private journal and had no luck. The closest I found so far was this guide from buddyforms. In terms of functionality, this is pretty close to what I need. I tried it on a setup with Buddyboss instead but the code somehow affects the menu and hides them completely. I tried in on a bare WP 2021 theme and it seems to be working but when I start working on a premade theme, issues start coming up.

add_action( 'pre_get_posts', 'buddyforms_filter_cpt_listing_by_author' );
function buddyforms_filter_cpt_listing_by_author( $wp_query_obj ) {

    // First let us check if this is a page. We not want to restrict pages
    if( !isset( $wp_query_obj->query ) || isset( $wp_query_obj->query['page'] ) || 'page' == $wp_query_obj->query['post_type'] ){
        return $wp_query_obj;
    }

    // Check if the user is logged in and display a page to logged off users
    // Just change the page_id to the page you want to display 
    // If you want to only restrict a custom post type, move this block to line 23
    if( ! is_user_logged_in() ) {
        $wp_query_obj->set('post_type', 'page' );
        $wp_query_obj->set('page_id', '2' );
        return $wp_query_obj;
    }

    // If you want to have the Author Only Posts work only with a custom post type uncomment this 3 lines and change "custom post type" to your post type.
//  if( ! isset( $wp_query_obj->query['post_type'] ) && 'custom post type' != $wp_query_obj->query['post_type'] ){
//      return $wp_query_obj;
//  }
    
    // Let us get the logged in user
    $current_user = wp_get_current_user();

    // If the user is not administrator or can at lest delete posts show all posts
    if( !current_user_can( 'delete_plugins' ) ){
        return $wp_query_obj->set('author', $current_user->ID );
    }
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.