4

I'm building a site for a client and we're using WordPress' native password protect functionality.

Is there a filter or some kind of action to filter posts retrieved in the administration panel? If I want to stop all password protected posts from being displayed in the admin panel, lets say if you're not an administrator.

1 Answer 1

3

You can use the has_password parameter of WP_Query.

Here's an example how you can hide it, for non administrators, on the edit.php screen for the post post type:

/**
 * Hide password protected posts, for non-admins, in the case of 'edit-post' screen id
 *
 * @link http://wordpress.stackexchange.com/a/200426/26350
 */
add_action( 'pre_get_posts', function( \WP_Query $q )
{
    $screen = get_current_screen();

    if(    is_admin() 
        && ! current_user_can( 'manage_options' ) 
        && is_a( $screen, '\WP_Screen' ) 
        && 'edit-post' === $screen->id
    )
        $q->set( 'has_password', false );
} );

Hopefully you can adjust this to your needs.

PS: it would be handy to have a get_current_screen_id() function, similar to the get_current_user_id() function ;-)

Update

/**
 * Hide password protected posts, for non-admins, in the case of 'edit-post' screen id. 
 * Here we don't restrict this to the current user.
 *
 * @link http://wordpress.stackexchange.com/a/200426/26350
 */
is_admin() && add_action( 'posts_where', function( $where, \WP_Query $q )
{
    $screen = get_current_screen();

    if(     
           ! current_user_can( 'manage_options' ) 
        && is_a( $screen, '\WP_Screen' ) 
        && 'edit-post' === $screen->id
    ){
        global $wpdb;
        $uid = get_current_user_id();
        $where .= $wpdb->prepare(
            " AND ( ( {$wpdb->posts}.post_password = '' AND {$wpdb->posts}.post_author != %d )
              OR ( {$wpdb->posts}.post_author = %d ) ) ",
            $uid,
            $uid
        );
    }
    return $where;
}, 10, 2 );

If WP_Query would support field_query:

It would be handy if WP_Query would support the field_query input argument, to make it easier to work with the post fields.

Here's an example:

add_action( 'pre_get_posts', function( \WP_Query $q )
{
    $screen = get_current_screen();

    if(    is_admin() 
        && ! current_user_can( 'manage_options' ) 
        && is_a( $screen, '\WP_Screen' ) 
        && 'edit-post' === $screen->id
    )
        $q->set( 'field_query', 
            [
                'outer_relation' => 'AND',
                'relation'       => 'OR',
                [ 
                    'field'   => 'author',
                    'value'   => get_current_user_id(),
                    'compare' => '=',
                ],
                [
                    'relation'  => 'AND',
                    [
                        'field'     => 'author',
                        'value'     => get_current_user_id(),
                        'compare'   => '!=',
                    ],
                    [
                        'field'     => 'password',
                        'value'     => '',
                        'compare'   => '=',
                    ],
                ]       
            ]
        );
} );

I've a draft for such a plugin, so maybe I will post it here when it reaches the alpha stage ;-)

8
  • Thanks a lot, I'll try this out in a few minutes. I thought edit.php was all post types? Is it possible to add this for all post types?
    – Dueify
    Sep 2, 2015 at 9:09
  • Somehow WordPress turns its familiar "white screen" on when entering the code.
    – Dueify
    Sep 2, 2015 at 9:19
  • Also is it possible inside pre get posts to determine the author of the post?
    – Dueify
    Sep 2, 2015 at 9:41
  • sorry about that, just forgot the quotes around \WP_Screen, just tested it and it works now @Dueify
    – birgire
    Sep 2, 2015 at 9:42
  • what kind of author filtering do you mean? @Dueify
    – birgire
    Sep 2, 2015 at 9:48

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.