WordPress Get Post ID by various parameters
This tutorial helps you to find the POST ID by various ways of various need. During theme or plugin developments we may need to get the post id on demand by some specific parameter like ‘current post’, ‘current page’ etc.
So here i tried to sort out all of theme.
GET POST ID OF CURRENT PAGE/POST:
# This function works anywhere outside of loop.
<?php $postid = get_the_ID(); ?>
# If you want get POST ID inside the loop you can use:
<?php the_ID(); ?>
GET POST ID BY TITLE:
specially uses for getting page id.
$pageName = get_page_by_title( 'About' ); $pageID = $pageName->ID;
GET POST ID OF FRONT PAGE (HOME PAGE):
$front_page_id = get_option( 'page_on_front' );
GET POST ID OF POSTS PAGE (BLOG PAGE):
$pageID = get_option('page_for_posts');
GET POST ID BY SLUG:
function get_ID_by_slug($page_slug) { $page = get_page_by_path($page_slug); if ($page) { return $page->ID; } else { return null; } } $postID = get_ID_by_slug('my-post');
GET POST ID BY URL:
<?php $postid = url_to_postid( $url ); ?>
Hope this post helps you lot to find out basic ways to GET POST ID in various requirements. Still if you need any other wasy then feel free to drop a message. We will answer you shortly for your solution.
Thanks.
This post has already been read 10233 times!
Hello,
I want post id by url not by published url not by permalink.
this function not working.
please help..