X

How to Hide Posts and Pages in the WordPress Admin Panel

Snippets by IsItWP

Do you want to hide posts and pages in Your WordPress admin panel? By hiding some posts and pages in your admin panel, you can restrict your clients from editing them. This comes in handy if you want to have some pages on your site ONLY for your templates in which you need to add shortcodes or something else that no one should edit.

This snippet allows you to specify the IDs of the posts and pages that you want to hide in WordPress dashboard.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

<?php
add_action('admin_head', 'hide_posts_pages');
 
function hide_posts_pages() {
    global $current_user;
    get_currentuserinfo();
    If($current_user->user_login != 'admin') {
        ?>
        <style>
           #post-10, #post-11, #post-12, #post-13, #post-14{
                display:none;
           }
        </style>
        <?php
    }
}
?>

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: Best WordPress Page Builders (Compared).

Comments   Leave a Reply

  1. Ignore my last request, I have sorted it!

  2. This is a really good snippet, just what I was looking for but it is hiding the pages from all users including me (admin), how can I hide from a selected set of user admins that I have created using User Role Editor. Thanks in advance!

  3. SCHALK thank you for help, can you post the function with one post and 1 page to see how will be the final cone?

    Thanks

    1. I apologize, I’m not sure if I understand the question.

  4. Hi,
    Is it possible to first add a class to each page, so that i can rather hide the injected class?

    the same way i can assign the page slug as a class to a page with the code below.
    The reason is, i wat ti make it easier to programatically hide pages if there are an options page present.

    So instead of #post-10 it will be .post-about or .post-contact

    function add_slug_body_class( $classes ) {
    global $post;
    if ( isset( $post ) ) {
    $classes[] = $post->post_type . ‘-‘ . $post->post_name;
    }
    return $classes;
    }
    add_filter( ‘body_class’, ‘add_slug_body_class’ );

    Thank you.

    Schalk

  5. Selçuk Ahmet July 23, 2017 at 7:40 am

    hahaaa after 5 years still kicks wordpress ass

  6. GREAT SOLUTION!!! I SEARCHED A LOT TO FIND THIS !!! THANK’S!!!

  7. Cool! thanx 🙂

  8. Yes this would be a better solution thanks for posting.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!