Monday, 26-09-2022

5 examples of getting Post by ID & 11 Ways to Get Post ID in WordPress

If you are looking for information on how to get post by ID, all you need is to use the get_post() function. This article guides you through some specific cases on how to get post content by ID in WordPress and extended cases such as: get the title, get category name, get post date, get term by post id.

5 examples of getting post by id

get post by ID in WordPress is a type of query to get information of one or more posts based on the post’s ID. Here are the 3 most common ways to get posts by ID in WP

Get post status by post id

There are many ways to get post status by post id, but the best way is to use the get_post_status function.

Get post type by id

We can use the get_post_type function to Get post type by id:


WordPress post content by ID

The first question is how can I get WordPress post content by post id? The way to do is quite simple is to use get_post() to get the data and then use apply_filters() to filter. Here is a sample example of getting post content by ID in WordPress

The same way to get specific page content. This is the proper way to get page content

The second way is to use WP_Query to display the content of a custom post type based on its ID.

Wordpress get post by ID
In this example, I want to get the content from the post with ID 55.
There is a function in there to get the featured image URL.
The example below will display the title, featured image with size 1280×800, and the full content of the post.

Get post title and post content by ID

To get the title for a post with ID 100:

To get the post content for a post with ID 100:

The second way is to use the get_post_field() function to get the data

The syntax of this function:

So code, that will get the title by post-id 100 looks like this:

Because of that, you want to display the title, so you can ignore the last param

Similarly, you can get the post content from the post_content field

Here are the default post fields that you can get (case-sensitive):

The above methods can be applied to many different post_types such as post, page, or custom post type

Get posts by multiple post IDs?

You can use get_posts() as it takes the same arguments as WP_Query.
To pass it the IDs, we use ‘post__in’ => array(25,28,65,72,86) (remember to put it in the array).
Below is an example of getting content for posts with IDs of 25,28,65,72,86. Foreach loop is used here to run through all found values

The second way is to use WP_Query. Here is an example

The result will show out like this:

  • Article Title 25
  • Article Title 28
  • Article Title 65
  • Article Title 72
  • Article Title 86

In short, there are 2 functions you can use to get post by ID, here are the 2 most basic examples to help you remember and practice
Method 1: use get_post() function, example:

Method 2: Use WP_Query

Or you can get post by ID with WordPress WP_Query too:

11 Ways to Get Post ID in WordPress

Next, we continue to look in the opposite direction, how to get Post ID in WordPress?
There are many ways to do this, depending on the specific case. Below I introduce 11 methods to get post-id of a post or a page with specific situations

Post ID in WordPress Database

In WordPress’s database, there is a table named wp_posts (yours may be different because the prefix of each website is different). When you click on the wp_posts table to view the information, there is a column called the ID column. You can find the Post ID of any post/page there

Post ID in phpmyadmin of WordPress Database

Post ID in PHPMyAdmin of WordPress Database

Add the Post ID column to the WordPress Posts Table

This second method is more professional when you add an ID column of the article to the page displaying the list of posts in the admin area. This way is quite professional and convenient, I also recommend using this way to display the ID of each post in the admin area.

You also don’t need to know the code, because I already wrote the code below, your task is just to copy and paste it into the last line of the functions.php file in your theme.

Add the Post ID column to the WordPress Posts Table

Add the Post ID column to the WordPress Posts Table

Get ID From the Global $post object

Note: The following examples all require some basic PHP knowledge. However, if you don’t know anything about the code, that’s okay, try and follow the detailed instructions below.

The $post global object contains all the data of the current post. The post ID is also present in $post, so getting the POST ID is pretty easy as we just need to output it:

I use global $post so that if you use it in functions or plugins it can still display the POST ID

Using get_the_id() and the_id() functions

Both the get_the_id() and the_id() functions return the post’s ID. The difference here is that the get_the_id() function returns the numeric post’s ID and does not display it, while the_id() function will always display the post’s ID.

With the get_the_id() function, we have to echo the new ID to display:

With the_id() function, we don’t need to echo because it’s already printed

Get Post ID by Title, SLug

Get Post ID by Title

This is a built-in WordPress function and since version 3.0.0 it works not only for pages but also for any custom post type. You can use this function anywhere in your theme, plugins, or functions

Get Post ID by SLug

The function is similar to get_page_by_title(). You just need to replace the title with the URL slug. One note is that if your URL has both category or parent, you need to fill in either category or parent.

If the URL has a parent or a category, for example, /en/ao-dai/, you must fill in the URL completely.

Get post ID from URL in WordPress

To Get post ID from URL in WordPress, just use the code below. Replace the example URL below ( with the URL you want to find the post’s ID

An easier way to get post ID in WP_Query loop

If you are inside the loop of WP_Query or any other loop, you can easily get the post ID by following way:

Get Post ID by meta key

Actually, there are two ways to Get Post ID by meta key – that is using the WP_Query query or MySQL query. The below function I show you will return the post ID or an array of post IDs that match the given meta key.

Get Post ID both by meta key and value

It is similar to the previous function, but here you can also specify a meta value.

Just like Get Post ID both by meta key, the function below will get the id of the post that satisfies BOTH conditions is meta key and meta value

Get Post ID by Term ID (Category ID, Tag ID)

Here is an example function that gets Get Post ID by Term ID. The term here can be category id or tag id

Usually, this function will return a multivalued array since the same term_id can have multiple articles attached to it. For example, with the same category id as 1, there can be hundreds of thousands of articles with the same topic. The same goes for tags.

In URL of the Post Without Custom Permalink Structure

To use this, you have to temporarily toggle your permalink settings in Settings > Permalink set to Plain. Now the p parameter in the URL is the post’s ID.

Post ID In URL of the Post Without Custom Permalink Structure

Post ID In URL of the Post Without Custom Permalink Structure

In URL on the post edit page

If you have access to post-editing, You can find the ID of the post in the URL when editing it.

ID In URL on the post edit page

ID In URL on the post edit page

You don’t even need to open the post edit page. Just move your mouse over the «Edit» link or over the post title in the admin area and then look at the browser’s status bar (bottom left part of the screen) you will see the edit URL. edit including the post’s ID

Get post id from browser status bar

Get post id from browser status bar

Get Post ID of Static FrontPage

A Static FrontPage is a specific Page used as the Home Page of the site.
If you want to get the ID of that static front page, you can use the following snippets to get the ID:

or get the ID of Posts page:

Above are 10 ways to get posts by ID in WordPress. At the end of the article, I will show you some ways to get article information based on post id such as: get the post edit time, post time, get term by post id, get current URL.

How to Get page id in WordPress?

If in the page want to get the ID, then we can use the get_the_id() function:

Or we can use get_page_by_path() function as below:

We can find the $post array of a Page with the title of “World Peace Now”:

How to get post ID by title in WordPress?

You can use the get_page_by_title() function to get the post ID based on the title
Examples are as follows:

Get featured image post in WordPress by post id

Here is the way to get a featured image of a post in WordPress by post id

If you want to show it as an image instead of a background, then you can use this code:

How to get a post id from permalink?

TO get the post-id from a permalink, you can use this function:


Frequently Asked Questions

How do I get posts by multiple post ID's?

You can get post data of multiple posts IDs using WP_Query or get_posts($args) function and pass those post ids into an array in post__in Eg:

$args = array(
'post__in' => array(43,23,65)
$posts = get_posts($args);
foreach ($posts as $p) :
//Get post data by $p->ID, $p->content!

How can I get WordPress post content by post id?

You simply use the get_post function to get the data and then filter it with the apply_filters() function. Finally, echo out. $my_postid = 12; //This is page id or post id $content_post = get_post($my_postid); $content = $content_post->post_content; $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); echo $content;

How to find WordPress Page ID and Post ID?

There are many ways to get Post ID or Page ID in WordPress such as Use Custom Code to Display Post IDs in The Posts Tab, Find The ID Within Each Post's URL, Find Post IDs Within the WordPress Database, Use Functions to Fetch WordPress Post IDs. .. Reread the "10 Ways to Get Post ID in WordPress" section of the "The Best Ways to Get Post by ID in WordPress" article to understand more about how to get the post ID

How to Get post ID from url wordpress?

If you are looking to Get post ID from WordPress URL, the simplest way is to use the WordPress url_to_postid() function. Example

echo url_to_postid(''); // Show ID of this post in number format: 2770

How to get post slug from post ID in WordPress?

If you're looking to get the post slug from the POST ID, there are two ways: Method 1: Using PHP alone Eg

 $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $url_path = parse_url( $url, PHP_URL_PATH ); $slug = pathinfo( $url_path, PATHINFO_BASENAME );
Method 2: Use the WordPress get_post function:
$post_id = 45; //specify post id here
$post = get_post($post_id);
$slug = $post->post_name;


About Author

Leave A Reply