Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@davidpaulsson
Created February 26, 2014 06:20
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save davidpaulsson/9224518 to your computer and use it in GitHub Desktop.
Save davidpaulsson/9224518 to your computer and use it in GitHub Desktop.
WordPress: Get page ID from slug
// Usage:
// get_id_by_slug('any-page-slug');
function get_id_by_slug($page_slug) {
$page = get_page_by_path($page_slug);
if ($page) {
return $page->ID;
} else {
return null;
}
}
@ricardobarantini
Copy link

Really useful. Thanks.

@maheshkamani
Copy link

Thank you very much. it's helpful to me.

@stuartjnelson
Copy link

Super helpful little function! Just been using it to make some dynamic breadcrumbs. Thanks for sharing.

@eddt
Copy link

eddt commented Dec 22, 2015

Great nugget of code here - I've forked it and extended it to also allow you to add the post type if necessary!
https://gist.github.com/eddt/ee1018f26f8fc195629a

@ReessKennedy
Copy link

Thanks! Helped me out.

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