WordPress Get Page ID From Page Slug


About The Author
Avatar Cabe Nolan is the founder of WP Cover where he shares his insight into WordPress, development, & entrepreneurship. Outside of WPCover, Cabe continues to run a successful WordPress development firm, Bold City Design as well as a few high profile websites, Arrivala, Two Way Resume, Dock Skipper, and a successful outdoors brand, DolfinPack.







WordPress Get Page ID From Page Slug

Super handy snippet of the day.  Ever need to get the WordPress page ID based on the slug of the page?  Yeah, me to! I use this function quite frequently, hopefully it can help you out.  To start, add the following function to your active themes function.php file:

// Get ID of page by slug
function get_id_by_slug($page_slug) {
	$page = get_page_by_path($page_slug);
	if ($page) {
		return $page->ID;
	} else {
		return null;
	}
}

Next, use this function on the frontend of your theme. You could do something like

//The following will output the title of the page we're grabbing
$page = get_id_by_slug( 'about' );
echo get_the_title( $page );

And there’s a little snippet for you that I bet will come in quite handy!


Share Your Thoughts

Leave a Reply

Your email address will not be published. Required fields are marked *


Related Stuff You Might Like







WordPress News, Tips, & Code Snippets

Join the WP Cover mailing list and get wordpress news, tips, code snippets, security warnings, and more delivered right to your inbox.  We won't flood your inbox, newsletters typically go out every 1-2 weeks unless it involves an important security release.

You have Successfully Subscribed!