How to get post, page or category ID in WordPress

How-to-get-post-page-or-category-ID-in-WordPress

WordPress post or page ID is very important to WordPress as it is the way to have a distinction to posts a computer can read, meaning, humanly speaking we can only read a posts or a page by its name or slug. One essential thing that a post or page ID is needed if you are going to retrieve a name, slug or even links from a category, posts or pages. Take for example if you want to get the title of a post from a query, you need to get the ID to dynamically get the title from the loop of query. There are other things you will be using the post or page ID and by the way, a post and a page have things in common, in terms of retrieving the ID. Now let us consider the methods below to get the ID of a post or a page.

Using get_the_ID() or $post->ID Method

This method is very common to retrieve the ID of your current post or page. Remember, it is the way to get the CURRENT post or page which means, you can retrieve its ID on the specific single post or page. In WordPress core function, the function is displayed on the function below which means it get the post or page ID from the current post if it is not empty.

function get_the_ID() {
    $post = get_post();
    return ! empty( $post ) ? $post->ID : false;
}

To use this function, copy the PHP code below.

<?php get_the_ID(); ?>

Optionally, you can use the code below which is more complicated.

<?php global $post $post_id = $post->ID; ?>

Using get_cat_ID() Method

This method or code is use to get the current ID of a specific category on WordPress. If you need to retrieve an ID from a category, especially when using it to get the category link, you should use get_cat_ID( ‘your-category-name’ ) like from the example below which get an ID from Blog category.

<?php
     $cat_ID = get_cat_ID( 'Blog' );
     $cat_link = get_category_link( $cat_ID );
     echo $cat_link; // Displays category link
?>

To use this function, use the code below.

<?php get_cat_ID( 'your-category-name' );?>

Using the slug method

This method or code retrieves the ID of a page from its slug. This is useful as well if you want to get the page link using slug like for the example below which get an ID from a page slug.

<?php
     $page_slug = get_page_by_path( 'your-slug' );
     $page_ID = $page_slug->ID;
     $page_link = get_page_link( $page_ID );
     echo $page_link; // Displays page link
?>

Conclusion

The ID of your page or post is very important to WordPress which makes a single post or page unique to each other. In the above tutorials, you can choose either of the methods depending on the needs of the given situation. If you need to get an ID of a post or page use the method get_the_ID() or $post->ID. If you need to need to get an ID for a category use the method get_cat_ID(). Otherwise if you need to get an ID from a page use the slug method. These methods can be different from others, but in my experience, these works fine based on the current WordPress coding practices. How about you, do you have other methods to get and ID for posts, pages or categories? Feel free to post your comments below.

Probewise

I create WordPress themes and plugins with simplicity for all people. I am also a blogger, layout artist and a computer technician.
Back to top