Last active
November 19, 2024 21:20
-
-
Save cartpauj/b7b7260f8f07484419ec95d5a29895d2 to your computer and use it in GitHub Desktop.
Hide protected content (posts, pages, cpts) from search and archive results
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Please note, this is not heavily tested, and may require some tweaking to get to work for your need | |
// This will also limit results found in the WP REST API as well | |
// If your result is large (50+ posts for example) - this is likely going to cause some server performance issues | |
// So I'd recommend using sparingly and remove the MeprRule::is_uri_locked(get_permalink($post) if you're not using any Custom URI Rules in MP | |
// If a search returns 10 results, but 8 of those are protected, then this will cause only 2 results to come back | |
// This could be problematic if you're paginating results, as some pages may have posts shown, and others may be completely blank | |
// And lastly, if your site has heavy object caching enabled, you may get mixed results. This likely will not work with object caching. | |
function filter_protected_posts_for_mp($posts, $query) { | |
if (!class_exists('MeprRule')) { return $posts; } | |
foreach ($posts as $key => $post) { | |
if (MeprRule::is_locked($post) || MeprRule::is_uri_locked(get_permalink($post))) { | |
unset($posts[$key]); | |
} | |
} | |
$posts = array_values($posts); | |
return $posts; | |
} | |
// Hook the function to the 'posts_results' filter | |
add_filter('posts_results', 'filter_protected_posts_for_mp', 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment