Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

How to redirect non-logged in users to login with post_password_required()

The function you are using– post_password_required— is true for password protected pages, not for private pages. I am honestly not sure which you mean, but if you do mean “private” as in most of your description then…

I am not aware of an is_private function but you can check the post object itself. There is a catch though. “Private” posts are rabidly protected. A user without permissions to see the post will get a 404. I think the following will work, but due to the nature of “private” posts this may still be buggy. It has not been rigidly tested.

function redirect_to_login() {
  global $wp_query,$wpdb;
  if (is_404()) {
    $private = $wpdb->get_row($wp_query->request);
    if( 'private' == $private->post_status  ) {
      wp_safe_redirect(home_url(get_permalink(188)));
      die;
    }
  }
}
add_action('template_redirect', 'redirect_to_login');

Related Posts:

  1. Correct method of redirecting user login
  2. Editing ‘Password Reset’ E-mail
  3. Change “You are now logged out” text
  4. Removing labels and tag on WordPress’s default login form
  5. Customize user account activation message
  6. How do we check if the user is logging in or registering?
  7. add filter login_redirect does not contain original requested redirect
  8. Change username before login
  9. Custom Login Errors and variables I can use
  10. Filters on Login Page
  11. How can I change the email that is inside the default email texts of wordpress?
  12. How do I hook my function to run only after submitting login form
  13. Change login CSS
  14. Filter wp_redirect() to stop redirect under certain condition
  15. Filter page title (displayed in browser tab) of wp-login
  16. How to redirect a unique link based on login status
  17. login_url filter creates permanent wp-admin -> wp-login redirection loop
  18. Login logout below menu bar
  19. Hook into ‘when user logs in’ [persistent login]
  20. Change password reqts with NO plugin without breaking resetpass link?
  21. Changing WooCommerce Display Price Based on User Role & Category [closed]
  22. Remove Editor From Homepage
  23. What do add_filters() and apply_filter() do?
  24. Password change when the user login first time
  25. What to use instead of the content_save_pre filter?
  26. How can I customize the wp_list_categories
  27. How can I reliably and globally disable wptexturize?
  28. Customise Jetpack Publicize text
  29. Can’t change a label in woocommerce with the normal filter
  30. Add classname to the_category() function?
  31. Filter the URL of next_posts_link & previous_posts_link
  32. Sensei LMS Hooks to Remove Content
  33. How to re-enable a filter after disabling with __return_false
  34. redirect pages with no content, instead of 404 error, using max_num_posts?
  35. Exclude One Category and its Subcategories using WP_LIST_FILTER
  36. Explanation for remove_filter used in the below code [closed]
  37. How to change the blog title with add_filter? details below
  38. Gutenberg disable the “block” tab in right sidebar
  39. When to use actions and when to use filters
  40. add_filter to ‘woocommerce_before_main_content’ [closed]
  41. Editing
  42. the_content filter – checking the post
  43. add_filter to apply_filters that exists within class
  44. Echoing Categories only if they’re a child of a given category
  45. Removal of Jetpack OG Tags from old blog site
  46. Adding a title prefix with the the_title filter
  47. How to removes all instances of thumbnails displayed in my theme?
  48. How to modify an existing meta description?
  49. Adding TinyMCE buttons without removing plugin buttons?
  50. Don’t see add_filter result in the site front page
  51. How can I include an existing custom sitemaps listing in wp-sitemap.xml?
  52. Display Externally Retrieved Data within a Custom Template
  53. Can the wordpress color palettes by changed through Javascript?
  54. Filter on the_content doesn’t update the content being searched via register_rest_route
  55. Where to add_filter in theme functions.php?
  56. tag removed not using a filter
  57. Give “Read More” precedence over excerpt() word count
  58. Adding a filter to comments_template
  59. How to customize category_description()?
  60. Add Bootstrap Classes to Recent Posts Widget
  61. Add filter to get_posts
  62. How can I output all apply_filters and do_action?
  63. Override has_post_thumbnail function
  64. Is it possible to override the default Gallery Settings form?
  65. Excerpt is being added above the content, but it should be after it
  66. WordPress add script with custom type (x-jquery-tmpl)
  67. Woocomemrce order and registration fileds to UPPERCASE
  68. How to combine multiple body classes
  69. How to replace username with email address in users table
  70. why require – does not load filter
  71. term_link filter rewrite link only applying half of condition?
  72. How to change dynamically page title according to variable data?
  73. How can I filter tag_description()?
  74. What is the action hook to use if you want to capture the new password during password change?
  75. Get_template_part inside filter?
  76. Need to return shortcode text instead of the output
  77. Dashboard filter based on custom fields URL response code
  78. I want to add string array to this function to filter displayed option in drpodown [closed]
  79. Remove style tags from head
  80. How to wrap pattern-matches from the_content in [wiki]-shortcode tags
  81. how to develop a filter in wordpress to let the user filter the page content depends on the date (newest to oldest etc…)?
  82. Print only parent categories of post in custom RSS feed
  83. Redirecting the lost password page request when using a custon login page
  84. Use add_filter inside another function
  85. How to filter reading/visibility posts based on role?
  86. Admin view: filter posts by whether they have specific taxonomy attached
  87. Filter posts by categories ajax is showing all the posts
  88. Incrementing content with extra text after save/publish
  89. Filter and validate user role in registration
  90. How to use a TinyMCE filter?
  91. Unable to get WordPress’s apply_filters(‘site_url’) to return a https scheme
  92. String Replace Caching & Chance of Collisions
  93. Admin filter/error if post title is too long
  94. What is the scope and persistence of add_filter() and remove_filter()?
  95. GET form action. Redirect to self
  96. After adding filter to plugin’s code, post’s content doesn’t display
  97. if i put the code in my theme’s function.php file can it work?
  98. Admin New Order: Autofocus on Search a product
  99. Is it possible to dynamically change the “page_for_posts” option?
  100. Why is the change in my query not taking into account
Categories filters Tags filters, login, wp-redirect
Have lots of meta for posts, is it better to get at all at once or each individually
wp_mail strips out the link url from the anchor tag in mail content

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress