I have a client who wants to close down access to the entire site, every post, category page, archive etc (while keeping the content), except for three specific pages (Start, about us, contact).

None of the membership plugins seem to address this, only logged in roles? I feel like this is the most simple thing ever, but I just can't seem to solve it.

Edit: Basically, what I'm saying is "if page x, x or x" continue, "else display message".


If you want to show a message, use this code in your functions.php file-

function se_236335_hide_content( $content ){
    $pages = array( 8, 26, 35 ); // allowed page IDs
    if( ! in_array( get_the_id(), $pages ) ){
        return 'Message here..';
    return $content;

add_filter( 'the_content', 'se_236335_hide_content' );

If you want a page redirection, use this-

function se_236335_redirect(){
    // allowed pages IDs
    $p1 = 9;
    $p2 = 11;
    $p3 = 35;
    // redirect location
    $location = 9;

    if( ! is_page( [ $p1, $p2, $p3 ] ) ) {
        wp_redirect( get_permalink( $location ) );
add_action( 'wp', 'se_236335_redirect' );
  • Well now it just exchanges the content with the message, ideally I would want a redirect on category pages, archives etc.
    – talven
    Aug 18 '16 at 19:39
  • You said- Basically, what I'm saying is "if page x, x or x" continue, "else display message".
    – mukto90
    Aug 18 '16 at 19:52
  • I have edited my answer. Please check.
    – mukto90
    Aug 18 '16 at 20:05
  • Yes I messed up my wording there, sorry for not being clear! This does what I want it to do. How could I modify it to apply to visitors and not logged in users?
    – talven
    Aug 18 '16 at 20:13
  • 3
    You could simplify with ! is_page( [ 9, 11, 35 ] ). In general we might also want the administrator to be able to view the site and login.
    – birgire
    Aug 18 '16 at 20:17

An alternative approach would be to use the $query object to modify the query accordingly

add_action('init', 'wpse_236335_hide_content' );
function wpse_236335_hide_content(){

  // Apply hook everywhere for non logged in users except on wp-login.php page
  if( ! is_user_logged_in() && $GLOBALS['pagenow'] !== 'wp-login.php' )
    add_action( 'pre_get_posts', 'my_permitted_content' );


function my_permitted_content( $query ){

  // If we are on any of those pages, by ID, proceed as normal
  if( $query->is_page( array( 9, 11, 35 ) ) ) {

  // set query so nothing is returned. using trash because trashed post are not shown by design
  $query->set( 'post_status', 'trash' ); 

  // if we are not on the blog roll or the home page. redirect to home page.
  if( ! $query->is_home() || ! $query->is_front_page() ){
    wp_redirect( site_url() );

  • Returns nothing for me..
    – talven
    Aug 19 '16 at 7:10
  • What's not returning? I edited the answer because there was a typo, were you referring to that. Also, it's also possible (maybe even better) to hook later, so instead of hooking our first hook to init, we could hook to pre_get_posts with a priority of 5 and our second pre_get_posts hook, we change the priority to 20. Other than that, if you have issues with this code, be more specific, as I just tested it out and it works for me.
    – bynicolas
    Aug 19 '16 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.