Home Forums Backend Issues (wp-admin) Hide comments in backend for users who don\'t have custom field option set


Hide comments in backend for users who don\'t have custom field option set

  • I have a custom post type called “secure_blog”. Users have a custom field on their user’s page that’s called “user_intern_yes”. So if an admin ticks this option for a user (no matter which user role) this user can see the custom post types pages of “secure_blog” which is an “internal” area of the website.

    For me it was the easiest way to include an “internal” area without dealing with custom user roles etc. .

    Now I see there is one problem: The “secure_blog” custom post type has the ability for leaving comments. All these comments are visible in the comments section of the wordpress backend. Now this is a problem. How can I make this comments in the backend only visible to users who have the custom field “user_intern_yes” active on their user’s page?

    In frontend I do it with:

    // current user
    $user = new WP_User(get_current_user_id());
    // the custom field on the user's pages
    $showintern = get_field('user_intern_yes', 'user_'.$user->ID);
    // check if user has set $showintern field to "yes"
    if($showintern == 1) :
    // show stuff

    Any idea how to filter the wordpress backend comments section? So that only this users see the comments for the “secure_blog” custom post type?

    I have this function to hide the post type and the comments:

    function wpse28782_remove_menu_items() {
        $user = new WP_User(get_current_user_id());
        $showintern = get_field('user_intern_yes', 'user_'.$user->ID);
        if($showintern == 0) :
            remove_menu_page( 'edit.php?post_type=intern' );
            remove_menu_page( 'edit-comments.php' );
    add_action( 'admin_menu', 'wpse28782_remove_menu_items' );

    But this hides ALL comments in backend. I only want to hide the comments for the custom post type “secure_blog”.

  • You’re going to need to use pre_get_comments. Unfortunately, I cannot find much documentation on this. Nor can I find much information on WP_Comment_Query But it should allow you two alter the comments returned just like pre_get_posts. Here’s a general idea of what to do.

    1) See if the user can see the secure area, if yes then return without doing anything
    2) Get all the post ID’s for the posts in the secure area and add them to post__not_in

    I don’t know if this will work.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.