Skip to content

Instantly share code, notes, and snippets.

@vijayhardaha
Last active September 24, 2023 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vijayhardaha/dce94fd5a01e1a6474b9dc95f05d3354 to your computer and use it in GitHub Desktop.
Save vijayhardaha/dce94fd5a01e1a6474b9dc95f05d3354 to your computer and use it in GitHub Desktop.
WordPress: Hide pages from admin pages table using page slugs

Hide Pages from WordPress Admin Pages Table Using Page Slugs

In WordPress, there are times when you want to hide specific pages from the admin pages table in the WordPress dashboard. This code snippet provides a solution to achieve this by excluding selected pages based on their slugs.

How to Use the Code

Follow these steps to use the provided code in your WordPress project:

  1. Open Your Theme's functions.php File: Open the functions.php file of your WordPress theme using a code editor.

  2. Copy and Paste the Code: Copy the following PHP code and paste it at the end of your functions.php file:

    <?php
    /**
     * Get the hidden page ids for query.
     *
     * @return array
     */
    function vhk_custom_wp_get_hidden_page_ids() {
        // Define a unique cache key for transient.
        $cache_key = 'vhk_custom_hidden_page_ids';
    
        // Here, you can add query arguments to delete the cache when needed.
        // For example, use this URL: edit.php?post_type=page&vhk_custom_wp_del_cache=1
        // to delete the old cache when adding new page slugs.
    
        // Check for the query argument to delete the cache.
        if ( isset( $_GET['vhk_custom_wp_del_cache'] ) && ! empty( sanitize_key( $_GET['vhk_custom_wp_del_cache'] ) ) ) {
            delete_transient( $cache_key );
        }
    
        // Get the page ids from transient.
        $page_ids = get_transient( $cache_key );
    
        // Check if we retrieved the ids or not.
        if ( false === $page_ids ) {
            // If the transient data is not available, fetch the ids and save the transient.
            // Prepare an array of page slugs.
            $page_slugs = array( 'page-slug-1', 'page_slug_2' );
    
            // Use array_map to fetch an array of objects and filter out any null values.
            $pages = array_filter( array_map( 'get_page_by_path', $page_slugs ) );
    
            // Check for an empty array and use wp_list_pluck to get the IDs from objects.
            $page_ids = ! empty( $pages ) ? wp_list_pluck( $pages, 'ID' ) : array();
    
            // Save the data in the transient for future use. Data will expire after 1 day.
            set_transient( $cache_key, $page_ids, DAY_IN_SECONDS );
        }
    
        // Return the page ids.
        return $page_ids;
    }
    
    /**
     * Exclude some pages from the admin pages table.
     *
     * @param WP_Query $query Query instance.
     */
    function vhk_custom_wp_exclude_pages_from_admin( $query ) {
        global $pagenow, $post_type;
    
        if ( is_admin() && 'edit.php' === $pagenow && 'page' === $post_type ) {
            // Fetch the page ids to be hidden.
            $page_ids = vhk_custom_wp_get_hidden_page_ids();
    
            // Check if the ids array is not empty.
            if ( ! empty( $page_ids ) ) {
                $query->query_vars['post__not_in'] = (array) $page_ids;
            }
        }
    }
    add_filter( 'parse_query', 'vhk_custom_wp_exclude_pages_from_admin' );
  3. Save the File: Save the changes to your functions.php file.

  4. Usage:

    • To hide specific pages from the admin pages table, add their slugs to the $page_slugs array in the vhk_custom_wp_get_hidden_page_ids function.

    • To delete the cached data and refresh the hidden pages list, you can visit the WordPress admin pages list and use the query argument vhk_custom_wp_del_cache=1 (e.g., edit.php?post_type=page&vhk_custom_wp_del_cache=1).

Now, the specified pages will be excluded from the WordPress admin pages table, providing a clean and organized admin interface.

Conclusion

This code snippet is useful for WordPress developers and administrators who need to hide specific pages from the admin pages table based on their slugs. It offers a simple way to enhance the WordPress admin experience by customizing which pages are displayed in the table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment