Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
Coppa WPB
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Come nascondere i post protetti da password dal loop di WordPress

Nota editoriale: guadagniamo una commissione dai link dei partner su WPBeginner. Le commissioni non influenzano le opinioni o le valutazioni dei nostri redattori. Per saperne di più su Processo editoriale.

WordPress consente di creare post protetti da password. Recentemente uno dei nostri lettori ha chiesto se fosse possibile nascondere i post protetti da password dal sito. Per impostazione predefinita, WordPress nasconde il contenuto di un post protetto da password, ma mostra comunque il titolo del post con il prefisso “Protetto”. In questo articolo vi mostreremo come nascondere i post protetti da password dal ciclo di WordPress.

Perché nascondere i post protetti da password in WordPress?

Per impostazione predefinita, WordPress visualizza il post protetto da password con il titolo e il prefisso “protetto”. Gli utenti dovranno inserire la password per visualizzare il contenuto del post.

Password protected posts displayed on homepage and in widgets

Il titolo del post è visibile nella homepage, negli archivi, nel widget dei post recenti, ecc. Se si desidera mantenere alcuni contenuti completamente privati, questo non è l’ideale.

Non solo gli utenti che non hanno la password possono vedere il titolo del post, ma possono anche provare a inserire la password. Come tutti sappiamo, le password possono essere decifrate.

Detto questo, vediamo come nascondere i post protetti da password dal ciclo di WordPress, in modo che gli altri utenti non possano vederli.

Nascondere i post protetti da password in WordPress

È sufficiente aggiungere questo codice al file functions.php del vostro tema o a un plugin specifico per il sito.

function wpb_password_post_filter( $where = '' ) {
    if (!is_single() && !is_admin()) {
        $where .= " AND post_password = ''";
    }
    return $where;
}
add_filter( 'posts_where', 'wpb_password_post_filter' );

Questo codice modifica semplicemente la query inviata a WordPress utilizzando il filtro posts_where. Chiede a WordPress di recuperare tutti i post che non hanno una password.

Visitate il vostro sito e vedrete che i post protetti da password non sono più visibili nella homepage, negli archivi o nei widget come i post recenti.

Before and after hiding protected posts in WordPress

È comunque possibile visitare il post accedendovi tramite un URL diretto al post stesso.

L’esempio precedente nasconde i post protetti da password a tutti gli utenti. E se si gestisse un sito WordPress con più autori e si volesse che i post protetti fossero visualizzabili dagli utenti che hanno la possibilità di modificare i post privati?

Basta modificare il codice precedente con un altro tag condizionale, come questo:

function wpb_password_post_filter( $where = '' ) {
   if (!is_single() && !current_user_can('edit_private_posts') && !is_admin()) {
        $where .= " AND post_password = ''";
    }
    return $where;
}
add_filter( 'posts_where', 'wpb_password_post_filter' );

In questo esempio, controlliamo se un utente non può modificare i post protetti da password, quindi mostriamo solo i post che non hanno una password. In questo modo, tutti gli utenti con i ruoli di amministratore e di editore vedranno i post protetti da password sul front-end del sito.

Speriamo che questo articolo vi abbia aiutato a nascondere i post protetti da password dal ciclo di WordPress sul vostro sito. Potreste anche consultare il nostro tutorial su come cambiare il prefisso dei post privati e protetti in WordPress.

Se vi è piaciuto questo articolo, iscrivetevi al nostro canale YouTube per i video tutorial di WordPress. Potete trovarci anche su Twitter e Google+.

Divulgazione: I nostri contenuti sono sostenuti dai lettori. Ciò significa che se cliccate su alcuni dei nostri link, potremmo guadagnare una commissione. Vedi come WPBeginner è finanziato , perché è importante e come puoi sostenerci. Ecco il nostro processo editoriale .

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Il kit di strumenti WordPress definitivo

Ottenete l'accesso gratuito al nostro kit di strumenti - una raccolta di prodotti e risorse relative a WordPress che ogni professionista dovrebbe avere!

Reader Interactions

14 commentiLascia una risposta

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. David Brown says

    Thank you for explaining how to *really* do it in code; I wasn’t expecting that from a site called “wpbeginner.com”! (Still works in 2024 and WP 6.5.2, by the way.)

    So many sites pretend to offer solutions but really want you to use their plug-in that overcomplicates some simple thing and leaves your site exposed to any vulnerabilities they might introduce and never patch.

    • WPBeginner Support says

      It would depend on the site and plugin as plugins can be for simply adding the same code as code from an article without needing to know how to edit files which is why sites recommend them. Glad you found our site and guide helpful :)

      Admin

  3. Vincent Zhang says

    Thank you guys so much. This really helped me. I appreciate it. Please keep more of this type of posts coming that do not involve using a plugin.

  4. Mark says

    I created a site specific plugin and followed these steps and it worked for hiding my post on the “Posts” page of my site. However, the post is still visible on a Related Posts widget for each individual post. (Very similar to the image you have above, however my Password protected post is still visible.)

    Is there anything I can do to fix this?

  5. Alex says

    I wanted to make my own code adjustment to show the posts if you could read_private_posts.

    function remove_password_protected_posts( $where = ” ) {
    if (!is_single() && !current_user_can(‘read_private_posts’) && !is_admin() ) {
    $where .= ” AND post_password = ””;
    }
    return $where;
    }
    add_filter( ‘posts_where’, ‘remove_password_protected_posts’ );

    Great post as usual. Thanks.

  6. Harin says

    Hi guys

    I made a site specific plugin with the following code:

    If I try to attach a nextgen gallery to my post, the gallery doesn’t load, as soon as I disable the plugin, the nextgen gallery goes back to normal.

    Regards

  7. Brandon says

    Thanks for this snippet. So helpful!

    Regarding hiding these posts from the rss feed, I ran across this snippet.


    function rss_filter_protected($query) {
    if ($query->is_feed) {
    add_filter('posts_where', 'rss_filter_password_where');
    }
    return $query;
    }
    add_filter('pre_get_posts','rss_filter_protected');

  8. Chris says

    Thanks for that great tip!
    But are these posts hidden from the loop with your code snippet also hidden from the RSS feed?

Lascia una risposta

Grazie per aver scelto di lasciare un commento. Tenga presente che tutti i commenti sono moderati in base alle nostre politica dei commenti e il suo indirizzo e-mail NON sarà pubblicato. Si prega di NON utilizzare parole chiave nel campo del nome. Avremo una conversazione personale e significativa.