0

I have a membership website where my users can create their own page. I use below snippets in function.php to remove the options of published, trash etc. of other users and only show "mine".

/**
 * Remove the 'all', 'publish', 'future', 'sticky', 'draft', 'pending', 'trash' 
 * views for non-admins
 */
add_filter( 'views_edit-page', function( $views )
{
    if( current_user_can( 'manage_options' ) )
        return $views;

    $remove_views = [ 'all','publish','future','sticky','draft','pending','trash' ];

    foreach( (array) $remove_views as $view )
    {
        if( isset( $views[$view] ) )
            unset( $views[$view] );
    }
    return $views;
} );

It is working fine. When a user create a page, it is showing only the "mine" option.

But When a new user 1st time come to create a page (they have not created any page); they still can view the "All" options where all the published page (of other users) are shown. Also when they delete his all "mine" pages, they can view the "All" option.

So how to stop users to see the "all" option when they 1st time come to create a page or delete all their pages?

6
  • 2
    Modify the WP_Query based on author and use pre_get_posts action to list only current user's posts.
    – Max Yudin
    Commented Feb 7, 2018 at 16:11
  • Thank you for help. But actually I do not know that much coding. I just found those codes from a answer of stackexchange & used it. Can you help me with any such function.php snippet codes which I can use.
    – Deb
    Commented Feb 7, 2018 at 16:59
  • @MaxYudin you should write an answer from that comment
    – Tom J Nowell
    Commented Feb 7, 2018 at 17:41
  • @Deb any answer you get on this site will be technical, if you're looking for a copy paste solution, then this isn't the place, if the code answers the question but doesn't do exactly what's necessary, e.g. markup is different, you'll need to be able to change it yourself
    – Tom J Nowell
    Commented Feb 7, 2018 at 17:42
  • @TomJNowell, As a new member, I understand your concern. Anyway, Thanks.
    – Deb
    Commented Feb 7, 2018 at 17:48

1 Answer 1

0
<?php
add_filter('pre_get_posts', 'my_current_author_posts');

function my_current_author_posts( $query )
{
    global $pagenow;

    // Check if you are on the right Admin page, do nothing if not
    if( 'edit.php' != $pagenow || ! isset( $_GET['post_type'] ) || 'page' != $_GET['post_type'] || ! is_admin() ) {
        return;
    }

    // get current user Object
    $current_user = wp_get_current_user();

    // check user capabilities
    if( ! current_user_can( 'edit_others_pages' ) ) {

        // remove views for pages, see the function below
        add_action( 'views_edit-page', 'my_remove_views' );

        // set WP_Query 'Author' parameter
        $query->set( 'author', $current_user->ID );
    }

    return $query;
}

// function to remove views
function my_remove_views( $views )
{

    $views_to_remove = array(
        'all',
        'publish',
        'future',
        'sticky',
        'draft',
        'pending',
        'trash',
    );

    foreach ( $views_to_remove as $view_to_remove ) {
        unset( $views[$view_to_remove] );
    }

    return $views;
}
7
  • I used, but not worked. New users can still view pages from other authors. Anyway thanks for giving your time & writing this codes.
    – Deb
    Commented Feb 9, 2018 at 8:59
  • Check users' capabilities. Possibly, they can edit_others_posts.
    – Max Yudin
    Commented Feb 9, 2018 at 9:07
  • Oh, sorry. I've copied and changed my working code but forgot to change edit_others_posts to edit_others_pages. Fixed.
    – Max Yudin
    Commented Feb 9, 2018 at 9:23
  • I used, but still users can view other pages. Check this screenshot: prnt.sc/ica06m When some user have not created any page or delete all their page, they can view pages of other authors.
    – Deb
    Commented Feb 9, 2018 at 9:51
  • Check fixed if statement: 'page' != $query->post_type changed to ! isset( $_GET['post_type'] ) || 'page' != $_GET['post_type']. I personally have different solution working with CPTs, but this shoud work too.
    – Max Yudin
    Commented Feb 9, 2018 at 10:39

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.