Hiding Page by Title from Editing

I have a number of pages which are automatically generated. I then use a custom settings box to allow users to exchange the content on those pages.

Problem being, I don’t want users to be able to delete these specific pages. One of those pages is the “Disclaimer”. The reason I’m doing this is because I’m creating a multi-site legal blog network and each page must have a disclaimer under US laws.

Read More

How do I hide these auto-generated pages (like “Disclaimer,” below) from the dashboard by title, rather than ID?

// If there is no disclaimer page, generate one from its template
$page = get_page_by_title( 'Disclaimer' );
if(!$page)
{
        wp_insert_post(array(
            'post_name' => 'disclaimer',
            'post_title' => 'Disclaimer',
            'post_status' => 'publish',
            'post_type' => 'page',
            'post_author' => 1,
            'page_template' => 'page-disclaimer.php',
        ));
}

Related posts

2 comments

  1. You can hide pages using a filter on pre_get_posts. That can be done setting 'post__not_in' argument, but that argument need page ids. And you don’t know the id before the page is created.

    For that reason, tou can run an additional query to retrieve the ids based on the titles, or better on the slugs (i.e. ‘post_name’).

    add_action('pre_get_posts', 'hide_some_pages');
    
    function hide_some_pages( $query ) {
      if ( ! is_admin() ) return;
      $screen = get_current_screen();
      if ( $query->is_main_query() && $screen->id === 'edit-page' ) {
        // add the post_name of the pages you want to hide
        $hide = array('disclaimer', 'hiddenpage');
        global $wpdb;
        $q = "SELECT ID FROM $wpdb->posts WHERE post_type = 'page' AND post_name IN (";
        foreach ( $hide as $page ) {
          $q .= $wpdb->prepare('%s,', $page);
        }
        $tohide = $wpdb->get_col( rtrim($q, ',') . ")" );
        if ( ! empty($tohide) ) $query->set('post__not_in', $tohide);
      }
    }
    
  2. If you want to hide this one page from users in the dashboard you could try use something like this:

    function hide_disclaimer($query) {
        if ( ! is_admin() )
            return $query;
        global $pagenow, $post_type;
    
        if ( !current_user_can( 'administrator' ) && is_admin() && $pagenow == 'edit.php' && $post_type == 'page' )
            $query->query_vars['post__not_in'] = array( '1' ); // Enter your page ID(s) here
    }
    add_filter( 'parse_query', 'hide_disclaimer' );
    

    Of course you would have to determine your page ID and add it to the array. With this code you can add more pages to the array you wish to hide.

Comments are closed.