1

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.

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',
        ));
}
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);
  }
}
  • G. M. , thanks for this code. For some reason though, when I add it to my functions, I get the white screen of death across my website. Any idea what may be wrong with it? – AndrettiMilas Jan 18 '14 at 23:24
  • I'm still getting the white screen of death. :-/ – AndrettiMilas Jan 19 '14 at 15:50
  • @AndrettiMilas you're right again. It worked on backend, but break page on frontend. I fixed it (and this time tested and works well) – gmazzap Jan 20 '14 at 1:04
1

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.

  • Would I replicate this function for each page - how do I do multiple pages? Thanks for the code! – AndrettiMilas Jan 18 '14 at 19:52
  • Add this function to your functions.php file and then just add all pages you want to hide to the array. For example if you want to hide pages with ID 7, 15 and 26 you would write array( '7', '15', '27' ). – Borek Jan 18 '14 at 19:55
  • Is this possible with page titles instead? – AndrettiMilas Jan 18 '14 at 20:09
  • @AndrettiMilas 'post__not_it' accepts only id. Page titles can't be used safely because they are not unique. Using my answer you can use page slugs that is very similar. – gmazzap Jan 18 '14 at 20:41

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.