Skip to content

Instantly share code, notes, and snippets.

@travisfont
Forked from lcdsantos/wp-get_id_by_slug
Last active June 11, 2018 10:26
Show Gist options
  • Save travisfont/a49ab6f9299e565115bd0782ad82270a to your computer and use it in GitHub Desktop.
Save travisfont/a49ab6f9299e565115bd0782ad82270a 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;
}
}
// get the lastest post (this can also be used for custom post types through argument)
function get_latest_post()
{
return get_posts('post_type=post&numberposts=1')[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment