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. ‘Problem’ with login_redirect filter – why does it lose the redirect_to parameter
  22. wp reset password page – head inline script
  23. Disable emojicons introduced with WP 4.2
  24. How to remove a filter that is an anonymous object?
  25. WordPress hooks/filters insert before content or after title
  26. How to add defer=”defer” tag in plugin javascripts?
  27. add_action(), add_filter() before or after function
  28. apply_filters(‘the_content’, $content) vs do_shortcode($content)
  29. How do filters and hooks really work in PHP
  30. Trouble understanding apply_filters()
  31. What is the very earliest action hook you can call?
  32. How would one modify the filtering Gutenberg applies to pasted content?
  33. How can I modify the WordPress default widget output?
  34. Add custom options to the wplink dialog
  35. Remove classes from body_class
  36. what is __return_false in filters
  37. Explanation for apply_filters function and its variables
  38. Gutenberg: Is there a way to know if current block is inside InnerBlocks?
  39. How to reorder billing fields in WooCommerce Checkout template? [closed]
  40. Insert HTML just after tag
  41. the_content and is_main_query
  42. Changing WooCommerce Display Price Based on User Role & Category [closed]
  43. How to add a custom CSS class to core blocks in Gutenberg editor?
  44. How to show page content in feed?
  45. wp_headers vs send_headers. When to use each?
  46. Filter any HTTP request URI?
  47. How to Pass External Variables to Filters/Actions
  48. How to filter users on admin users page by custom meta field?
  49. Filter by one custom field, order by another?
  50. Not able to change wp_title using add_filter
  51. How to appending to the_content using add_filter with custom post type?
  52. Query WP REST API v2 by multiple meta keys
  53. No filter of code on switch from html to visual editor, how?
  54. Sanitize and data validation with apply_filters() function
  55. How to modify posts_where filter only for the search query
  56. How to hook a filter to catch get_post_meta when alternate a custom field output?
  57. How to get shortcode’s input values inside a filter?
  58. Removing Image and Caption Dimension Attributes
  59. How to wrap oEmbed-embedded video in DIV tags inside the_content?
  60. How to bulk delete all users with no posts?
  61. How many filter/action hooks are healthy?
  62. WordPress 3.9 – Trouble Editing TinyMCE 4.0
  63. Changing JPEG compression depending on image size
  64. How to add filter with 2 args?
  65. Why is javascript allowed in my post content?
  66. How to use update_{$meta_type}_metadata filter to modify meta value
  67. How to wrap an element around an iframe or embed in content automatically?
  68. Filter specific shortcode output?
  69. WordPress Internal @ Mentions
  70. How to add headers to outgoing email?
  71. Earliest hook to reliably get $post/$posts
  72. Insert new element to array with add_filter
  73. LESS CSS enqueue_style with add_filter to change rel attribute
  74. Is it possible to filter comments in a post so a user can only see the comments they have written?
  75. How to pass/get data to/from the WooCommerce data-product_variations object?
  76. Remove Editor From Homepage
  77. How to modify Contact Form 7 Success/Error Response Output [closed]
  78. Where to hook into post content?
  79. What does (10, 2) mean when used with add_filter
  80. Filter translations (gettext strings) on specific admin pages
  81. Filter username field on registration for profanity and unwanted words
  82. Clarification on filters and hooks
  83. remove_filter( ‘the_content’, ‘wpautop’ ); only for certain post types
  84. At what priority does add_filter overwrite core functions?
  85. Valid characters for actions, hooks and filters
  86. is it possible to add “extra” table nav to edit-tags.php screens?
  87. Advanced Custom Fields and Yoast SEO keyword analysis [closed]
  88. Adding a filter to qTranslate to change display of language chooser
  89. Insert Custom HTML After Shortcode
  90. Modify links when inserted by WYSIWYG editor
  91. Is there a way to add another row to the tinyMCE kitchen sink toggle?
  92. How to hook into unregistering a widget instance?
  93. How to check if a hook is hooked or not?
  94. Completely strip any hidden formatting when pasting into TinyMCE
  95. How can I extend the Gutenberg table block transform to allow colspans/rowspans on pasted table elements?
  96. How to add filter to __() and _e()?
  97. How to change the wp-login.php page title?
  98. Remove description from on Home
  99. Is it possible to use object in add_action?
  100. add_filter and changing output captions of image gallery
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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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