Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question
add comment

1 Answer

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;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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