1

Currently, WordPress does a great job displaying pages on the Edit Pages admin area for users with the capability edit_others_pages. However, if a user has edit_others_pages and has not yet authored any pages, WordPress defaults to listing all the pages removing edit actions, etc. However, I would like WordPress to display no pages if a person hasn't authored any pages.

I've tried writing some code into pre_get_posts, but it doesn't seem to work.

add_action( 'pre_get_posts', 'wps_admin_exclude_pages' );
function wps_admin_exclude_pages( $query ) {
    if( ! is_admin() )
        return $query;

    global $pagenow, $current_user;
    if( 'edit.php' == $pagenow && 'page' == get_query_var( 'post_type' ) && ! current_user_can( 'edit_others_pages' ) )
        $query->set( 'post_author', $current_user->ID );

    return $query;
}

1 Answer 1

1

I was using the wrong query_var. It's author not post_author.

add_action( 'pre_get_posts', 'wps_admin_exclude_pages' );
function wps_admin_exclude_pages( $query ) {
    if( ! is_admin() )
        return $query;

    global $pagenow, $user_ID;
    if( 'edit.php' == $pagenow && 'page' == get_query_var( 'post_type' ) && ! current_user_can( 'edit_others_pages' ) )
        $query->set( 'author', $user_ID );

    return $query;
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.