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

WordPressのループからパスワードで保護された投稿を非表示にする方法

編集メモ: WPBeginner のパートナーリンクから手数料を得ています。手数料は編集者の意見や評価に影響を与えません。編集プロセスについて詳しく知る。

WordPressでは、パスワードで保護された投稿を作成することができます。最近、ある読者から、パスワードで保護された投稿をサイトから非表示にすることはできないかという質問を受けました。初期設定では、WordPressはパスワードで保護された投稿のコンテンツを非表示にしますが、投稿タイトルのプレフィックスは’Protected’のまま表示されます。この投稿では、WordPressのループからパスワードで保護された投稿を非表示にする方法を紹介します。

なぜWordPressでパスワードで保護された投稿を非表示にするのか?

初期設定では、WordPressはパスワードで保護された投稿をタイトルと「protected」というプレフィックス付きで表示します。ユーザーは投稿のコンテンツを表示するためにパスワードを入力する必要があります。

Password protected posts displayed on homepage and in widgets

この投稿タイトルはホームページ、アーカイブ、最近の投稿ウィジェットなどで表示されます。一部のコンテンツを完全に非公開にしたい場合、これは理想的ではありません。

パスワードを持っていないユーザーが投稿タイトルを見ることができるだけでなく、パスワードを入力しようとすることもできます。すべてご存知のように、パスワードはクラックされる可能性があります。

そこで、パスワードで保護された投稿をWordPressのループから非表示にし、他のユーザーに見られないようにする方法を紹介しよう。

カテゴリー:WordPressでパスワードで保護された投稿を非表示にする

このコードをテーマのfunctions.phpファイルまたはサイト固有のプラグインに追加するだけです。

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' );

このコードは、posts_whereフィルターを使用してWordPressに送信されるクエリーを変更するだけです。パスワードを持たない投稿をすべて取得するようWordPressに依頼します。

あなたのサイトを訪問すると、パスワードで保護された投稿がホームページやアーカイブ、最近の投稿のようなウィジェットに表示されなくなっていることがわかります。

Before and after hiding protected posts in WordPress

投稿自体への直接URLからアクセスすることで、投稿を閲覧することは可能です。

上記の例では、パスワードで保護された投稿をすべてのユーザーから非表示にしています。もし、複数の投稿者がいるWordPressサイトを運営していて、非公開の投稿を編集できるユーザーが保護された投稿を閲覧できるようにしたいとしたらどうでしょうか?

上記のコードを別の条件分岐タグで次のように変更するだけです:

[cbk2]

この例では、ユーザーがパスワードで保護された投稿を編集できないかどうかをチェックし、パスワードが設定されていない投稿のみを表示します。こうすることで、管理者とエディターの権限を持つすべてのユーザーが、サイトのフロントエンドでパスワードで保護された投稿を見ることができます。

この投稿が、あなたのサイトのWordPressループからパスワードで保護された投稿を非表示にするのに役立てば幸いです。WordPressでプライベート投稿と保護された投稿の接頭辞を変更する方法についてのチュートリアルもご覧ください。

この投稿が気に入った場合は、WordPressの動画チュートリアルをYouTubeチャンネルに登録してください。Twitterや Google+でもご覧いただけます。

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 WPBeginnerの資金源 をご覧ください。3$編集プロセスをご覧ください。

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.

究極のWordPressツールキット

ツールキットへの無料アクセス - すべてのプロフェッショナルが持つべきWordPress関連製品とリソースのコレクション!

Reader Interactions

14件のコメント返信を残す

  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 :)

      管理者

  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?

返信を残す

コメントありがとうございます。すべてのコメントは私たちのコメントポリシーに従ってモデレートされ、あなたのメールアドレスが公開されることはありませんのでご留意ください。名前欄にキーワードを使用しないでください。個人的で有意義な会話をしましょう。