X

Limiter les auteurs à la visualisation des messages qu’ils ont créés

Snippets by IsItWP

Voulez-vous restreindre les auteurs à ne voir que les articles qu’ils ont créés dans le panneau d’administration ? Bien qu’il existe probablement un plugin pour cela, nous avons créé un extrait de code rapide que vous pouvez utiliser pour restreindre les auteurs à ne voir que les articles qu’ils ont créés dans WordPress.

Instructions:

Tout ce que vous avez à faire est d’ajouter ce code au fichier functions.php de votre thème ou dans un plugin spécifique à votre site:

function posts_for_current_author($query) {
        global $pagenow;
 
    if( 'edit.php' != $pagenow || !$query->is_admin )
        return $query;
 
    if( !current_user_can( 'manage_options' ) ) {
       global $user_ID;
       $query->set('author', $user_ID );
     }
     return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');

Note : Si c’est la première fois que vous ajoutez des extraits de code dans WordPress, veuillez consulter notre guide sur la façon d’ajouter correctement des extraits de code dans WordPress, afin de ne pas casser accidentellement votre site.

Si vous avez aimé cet extrait de code, n’hésitez pas à consulter nos autres articles sur le site comme : 15 meilleurs outils de marketing de contenu et comment créer un formulaire de contact sur WordPress.

Commentaires   laisser une réponse

  1. How do I do so it only shows current authors posts only but that it also shows another authors posts that is set manually by the id, in my case it is user id 3 I want all authors to see

    1. Hey Patrick, a simple solution is to create a password protected page for your authors and display posts from all authors on this page.

  2. Hello Chard, how to if thus will use for a page only not post, thanks need your feed back soon

    1. A page in WordPress is a type of post. That said, you may need to change ‘edit.php’ on line 4 to ‘edit.php?post_type=page’.

  3. can you help me with the code for Restricting authors to view only Category they created.
    Thanks

  4. Uldis Zalcmanis juillet 26, 2012 à 2:11 am

    is there any way to restrict author of comment to be the only who can see the comment? I know it sounds like a nonsense – don`t ask 🙂 I really need it! Thanks in advance.

  5. Thanks a lot Kevin!!

    1. No problem, glad I could help.

  6. Cliff Paulick mai 22, 2012 à 9:07 am

    Also, does this work for only posts or also pages?

  7. Using WP 3.3.1 and this is not working. Is their an update on the code? Thanks.

  8. Ernest, I would venture to guess you would replace:

    if( !current_user_can( ‘manage_options’ ) ) {

    with:

    if( !current_user_can( ‘level_1’ ) ) {

    Anyone with a role higher than Contributor would be able to see all posts.

  9. Hi this is a great snippet! How can I change the code to have this effect for the user role “Contributors”?

  10. This worked as described. But, when I removed it from functions.php I couldn’t see any posts when logged in as anything other than an admin. ???

    1. Sorry. Looks like it may have been an issue with something else. Seems to work OK now.

      1. Hi Chad  Cool glad to see you got things working.

  11. Great snippet, thanks. Do you know a way to restrict visibility of the sub menu headings by user ID. i.e. the author of a post that is of custom_post_type A, only sees the sub menu for custom_post_type A while the other custom_post_type sub menus are hidden?

    1. Hi Craig glad you like the snippet. In regards to the sub menu this snippet should solve this for you. http://wpsnipp.com/index.php/functions-php/remove-sub-menu-dashboard-items/

  12. Thanks for this 😉 

    Does it work for all user classes (Authors, Editors etc? ) 

    1. This snippet will only display the posts create by that user should work for everyone. So if you have a user that created no posts they should see nothing within the posts admin view.

Ajouter un commentaire

Nous sommes heureux que vous ayez choisi de laisser un commentaire. N'oubliez pas que tous les commentaires sont modérés conformément à notre privacy policy, et que tous les liens sont en nofollow. N'utilisez PAS de mots-clés dans le champ du nom. Engageons une conversation personnelle et constructive.

WordPress Launch Checklist

L'ultime liste de contrôle pour le lancement de WordPress

Nous avons rassemblé tous les éléments essentiels de la liste de contrôle pour le lancement de votre prochain site Web WordPress dans un ebook pratique.
Oui, envoyez-moi le gratuit !