How to remove password protected posts from pagination or adjacent posts

You can approach this in different ways. The easiest solution would probably use the WordPress default filters

There are multiple filters to remove password protected posts from adjacent posts for pagination.

Sometimes we need to hide all password protected links from pagination because some website creates the secure content data using private posts. If anyone navigates the private posts it will show the “Enter password form” but this is not secure for secure links because some time robot can hack this links easiest way, so those password protected links we can hide form loop an pagination.

We can achieve solution number of ways but standards way of using the WordPress filters

1.get_previous_post_where
2.get_next_post_where

1. get_next_post_where

function get_next_post_where (which finds the post after the currently-displayed post), applied to the SQL WHERE clause (which normally looks for the next dated published post). Filter function arguments: WHERE clause, stay in the same category (true/false), list of excluded categories.

2. get_previous_post_where

function get_next_post_where (which finds the post before the currently-displayed post), applied to the SQL WHERE clause (which normally looks for the previous dated published post). Filter function arguments: WHERE clause, stay in the same category (true/false), list of excluded categories.

Example code or remove password protected posts from pagination


add_filter('get_previous_post_where', 'remove_password_post_links_adjacent');
add_filter('get_next_post_where', 'remove_password_post_links_adjacent');
function remove_password_post_links_adjacent($where)
{
return $where ." AND post_password = '' ";
}

Copy above snippet and paste in functions.php file

start a project with us