Add Snippet To Project
Do you have pages on your WordPress website that you don’t want to be visible to the public? Maybe you have a page that’s still in development or a page that’s meant for internal purposes only. Whatever the reason, it can be helpful to know how to hide pages on WordPress. In this article, we’ll show you a few ways to do it.
function wpturbo_hide_pages() {
global $wpdb;
$page_slug = 'example-page'; // replace with your page slug
$page_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type = 'page'", $page_slug ) );
if( ! $page_id ) {
return;
}
wp_list_pages( array(
'title_li' => '',
'exclude' => $page_id,
) );
}
In WordPress, it is sometimes necessary to hide specific pages from the navigation menu or other parts of the website. The code snippet provided here offers an easy way to hide a specific page on your WordPress site.
First, we define a new function called wpturbo_hide_pages()
. Inside the function, we define the page slug of the page we want to hide. You should replace ‘example-page’ with the slug of the page you want to hide.
Next, we use the global $wpdb
object to query the WordPress database for the ID of the page that matches the given slug. We do this by using the prepare()
method to securely sanitize the page slug and then search for the page in the $wpdb->posts
table. If the page ID is not found, the function simply returns and does not affect the output.
If the page ID is found, we use the wp_list_pages()
function to output a list of all pages except the one we want to hide. We accomplish this by using the exclude
parameter of wp_list_pages()
, which takes a comma-separated list of page IDs to exclude from the output.
We set the title_li
parameter to an empty string to remove the default "Pages" title from the output. If you want to keep the title, simply remove this parameter.
Overall, this code snippet is a simple and effective way to hide a specific page from the navigation menu or other parts of your WordPress website.