Categories: Wordpress Development

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 10314 times!

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

More Posts

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

Share
Published by
Mehedi Hasan

Recent Posts

WP Share A Friend – A Way to Capture Lead

What is Lead Capture: In general sense lead capture designates a process or way of…

5 years ago

Unlimited Modal POPUPS on MouseClick

Introduction Unlimited Modal POPUPS on MouseClick: Everyone knows about popup during site visits and most…

5 years ago

WordPress Pregnancy Calculator Plugin

Brief about Pregnancy Calculator : This WordPress Pregnancy Calculator plugin allows users to display an…

5 years ago

Interactive Service Plugin with Hover Effects VC

Introduction About this Service Plugin: This Visual Composer Service Plugin works as an addon to…

6 years ago

WP Dynamic Query String

Introduction of Dynamic Query String : This plugin allows user to set dynamic query string…

6 years ago

How to make scroll PDF in iFrame for iPAD Safari

There is an occasional bug for iPhone and iPad even they are designed as well and…

7 years ago