Deseja restringir os autores a visualizarem somente as publicações que criaram no painel de administração? Embora provavelmente exista um plug-in para isso, criamos um trecho de código rápido que você pode usar para restringir os autores a visualizar apenas as publicações que eles criaram no WordPress.
Instruções:
Tudo o que você precisa fazer é adicionar esse código ao arquivo functions.php do seu tema ou em um plug-in específico do 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');
Observação: Se esta é a primeira vez que você adiciona trechos de código no WordPress, consulte nosso guia sobre como adicionar corretamente trechos de código no WordPress para não danificar acidentalmente seu site.
Se você gostou desse snippet de código, considere dar uma olhada em nossos outros artigos no site, como: 15 melhores ferramentas de marketing de conteúdo e como criar um formulário de contato no WordPress.
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
Hey Patrick, a simple solution is to create a password protected page for your authors and display posts from all authors on this page.
Hello Chard, how to if thus will use for a page only not post, thanks need your feed back soon
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’.
can you help me with the code for Restricting authors to view only Category they created.
Thanks
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.
Thanks a lot Kevin!!
No problem, glad I could help.
Also, does this work for only posts or also pages?
Using WP 3.3.1 and this is not working. Is their an update on the code? Thanks.
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.
Hi this is a great snippet! How can I change the code to have this effect for the user role “Contributors”?
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. ???
Sorry. Looks like it may have been an issue with something else. Seems to work OK now.
Hi Chad Cool glad to see you got things working.
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?
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/
Thanks for this 😉
Does it work for all user classes (Authors, Editors etc? )
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.