How to Exclude Pages from the Edit List in WordPress by ID

Home » Snippets » How to Exclude Pages from the Edit List in WordPress by ID
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you tired of scrolling through a long list of pages every time you want to edit a specific page in WordPress? If so, then this article is for you. In WordPress, there is a way to exclude certain pages from the edit list by their ID. This means that you can easily access the pages you frequently edit without the hassle of searching for them. In this article, we will guide you through the steps on how to exclude pages from the edit list by their ID, making your editing process more efficient and streamlined.

					function wpturbo_exclude_pages_from_edit_list($query) {
    global $pagenow, $post_type;
    if (is_admin() && $pagenow == 'edit.php' && $post_type == 'page') {
        $exclude_ids = array(1, 2, 3); // Replace with the IDs of the pages you want to exclude
        $query->set('post__not_in', $exclude_ids);
    }
}
add_action('pre_get_posts', 'wpturbo_exclude_pages_from_edit_list');
				

The code snippet provided above allows you to exclude specific pages from the edit list in the WordPress admin dashboard. This can be useful if you have certain pages that you don’t want to be easily accessible for editing.

To achieve this, we define a function called wpturbo_exclude_pages_from_edit_list() that takes in the $query parameter. This function is hooked into the pre_get_posts action, which allows us to modify the query before it fetches the list of pages to be displayed in the admin.

Inside the function, we first check if we are in the admin area and if the current page is the edit.php page for the post type "page". This ensures that the exclusion only happens on the page editing screen.

Next, we define an array called $exclude_ids which contains the IDs of the pages that we want to exclude from the edit list. You should replace the placeholder IDs (1, 2, 3) with the actual IDs of the pages you want to exclude. You can find the page ID by editing the page and looking at the URL in your browser’s address bar.

After setting up the array of excluded IDs, we use the $query->set() method to modify the query and exclude the specified pages. The post__not_in parameter is set to the $exclude_ids array, which tells WordPress not to include those pages in the query results.

By hooking this function into the pre_get_posts action, the exclusion will be applied before the query fetches the list of pages to be displayed, effectively hiding the specified pages from the edit list in the WordPress admin dashboard.

Remember to replace the placeholder IDs with the actual page IDs you want to exclude, and you’re all set!

Register an account to save your snippets or go Pro to get more features.