Skip to content

Instantly share code, notes, and snippets.

@KorvinM
Last active June 25, 2022 15:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KorvinM/1d3da2b9e7a239a28278 to your computer and use it in GitHub Desktop.
Save KorvinM/1d3da2b9e7a239a28278 to your computer and use it in GitHub Desktop.
WordPress: Remove private posts from the main query on the posts page
<?php
/* We can use the pre_get_posts action hook to modify a query before it's run.
* http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts*/
add_action( 'pre_get_posts', 'kvn_rem_pp' );
function kvn_rem_pp($query){
if(current_user_can ('read_private_posts') ){
/*restrict to the posts page main query.
*All other views (eg. category archives) unnaffected
*http://codex.wordpress.org/Function_Reference/is_home
*http://codex.wordpress.org/Function_Reference/is_main_query*/
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'post_status', 'publish' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment