{"id":41765,"date":"2024-11-25T21:12:11","date_gmt":"2024-11-25T19:12:11","guid":{"rendered":"https:\/\/coma.lv\/2024\/11\/25\/how-to-hide-password-protected-posts-from-the-wordpress-loop\/"},"modified":"2024-11-25T21:12:11","modified_gmt":"2024-11-25T19:12:11","slug":"how-to-hide-password-protected-posts-from-the-wordpress-loop","status":"publish","type":"post","link":"https:\/\/coma.lv\/en\/2024\/11\/25\/how-to-hide-password-protected-posts-from-the-wordpress-loop\/","title":{"rendered":"How to Hide Password-Protected Posts from the WordPress Loop"},"content":{"rendered":"\n<p style=\"text-align: left;\">By default, WordPress hides the content of password-protected posts, but it still shows the post title with the Protected prefix. In this article we will show you how to hide password-protected posts from the WordPress loop.<\/p>\n<h2 style=\"text-align: left;\">Why Hide Password-Protected Posts?<\/h2>\n<p style=\"text-align: left;\">The title of a password-protected post still shows up on the site&#8217;s home page, in the recent posts list, in archives and so on. If you want maximum content privacy, this is not great. It also gives users who do not have passwords a chance to try guessing them.<\/p>\n<h2 style=\"text-align: left;\">Hiding Password-Protected Posts in WordPress<\/h2>\n<p style=\"text-align: left;\">Just add this code to the functions.php file<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction wpb_password_post_filter( $where = &amp;#039;&amp;#039; ) {\r\n    if (!is_single() &amp;amp;&amp;amp; !is_admin()) {\r\n        $where .= &amp;quot; AND post_password = &amp;#039;&amp;#039;&amp;quot;;\r\n    }\r\n    return $where;\r\n}\r\nadd_filter( &amp;#039;posts_where&amp;#039;, &amp;#039;wpb_password_post_filter&amp;#039; );\r\n<\/pre>\n<p style=\"text-align: left;\">All this code does is change the query sent to WordPress using the posts_where filter. It tells WordPress to collect all posts that have no password. Go to your site and you will see that your password-protected posts are no longer visible on the home page, in archives or in widgets.<\/p>\n<p style=\"text-align: left;\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/coma.lv\/wp-content\/uploads\/2016\/11\/hiding-protected-posts.jpg\" alt=\"WordPress\" title=\"\"><\/p>\n<p style=\"text-align: left;\">You can still reach a post through a direct link to it. The example above hides password-protected posts from all users. What if you run a multi-author site and want password-protected posts to be visible to users with permission to edit private posts? Just modify the code above with a different tag:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction wpb_password_post_filter( $where = &amp;#039;&amp;#039; ) {\r\n   if (!is_single() &amp;amp;&amp;amp; !current_user_can(&amp;#039;edit_private_posts&amp;#039;) &amp;amp;&amp;amp; !is_admin()) {\r\n        $where .= &amp;quot; AND post_password = &amp;#039;&amp;#039;&amp;quot;;\r\n    }\r\n    return $where;\r\n}\r\nadd_filter( &amp;#039;posts_where&amp;#039;, &amp;#039;wpb_password_post_filter&amp;#039; );\r\n<\/pre>\n<p style=\"text-align: left;\">In this example we check whether the user can edit password-protected posts. If not, they are shown only the posts that have no passwords.<\/p>\n<hr \/>\n<p style=\"text-align: left;\"><em>Our specialization is WordPress website development and maintenance. Contact us for a free consultation \u2014 <a href=\"mailto:vadim@coma.lv\">vadim@coma.lv<\/a>, <a href=\"tel:+37129394520\">+371 29394520<\/a><\/em><\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>By default, WordPress hides the content of password-protected posts, but it still shows the post title with the Protected prefix. In this article we will show you how to hide password-protected posts from the WordPress loop. Why Hide Password-Protected Posts? The title of a password-protected post still shows up on the site&#8217;s home page, in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":38225,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1225,1406],"tags":[],"class_list":["post-41765","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","category-for-beginners"],"_links":{"self":[{"href":"https:\/\/coma.lv\/en\/wp-json\/wp\/v2\/posts\/41765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coma.lv\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coma.lv\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coma.lv\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coma.lv\/en\/wp-json\/wp\/v2\/comments?post=41765"}],"version-history":[{"count":0,"href":"https:\/\/coma.lv\/en\/wp-json\/wp\/v2\/posts\/41765\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coma.lv\/en\/wp-json\/wp\/v2\/media\/38225"}],"wp:attachment":[{"href":"https:\/\/coma.lv\/en\/wp-json\/wp\/v2\/media?parent=41765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coma.lv\/en\/wp-json\/wp\/v2\/categories?post=41765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coma.lv\/en\/wp-json\/wp\/v2\/tags?post=41765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}