How to Get Post and Page IDs in WordPress (5 Methods)

1. Use the ID Column in the WordPress Admin Area: The easiest way to get the post or page ID in WordPress is to simply look at the list of posts or pages in the WordPress admin area. Each post and page has an ID column that displays the ID number.

2. Use the Post/Page ID Column in the WordPress Admin Bar: If you are logged in to your WordPress site, you can also view the post or page ID in the WordPress admin bar. When you hover over a post or page title, the ID will be displayed in the admin bar.

3. Use the get_the_ID() Function: If you are working with a WordPress theme or plugin, you can use the get_the_ID() function to get the post or page ID. This function will return the ID of the current post or page.

4. Use the get_post_field() Function: The get_post_field() function can be used to get the post or page ID. This function takes two parameters: the post field and the post ID.

5. Use the get_page_by_title() Function: The get_page_by_title() function can be used to get the post or page ID by its title. This function takes the title of the post or page as a parameter and returns the ID.

There are a lot of situations when you might need to know what a specific page or post’s ID is in WordPress. A plugin might need it to exclude that page from a feature, for example. Alternatively, if you’re a developer, you could have to query that ID.

Page and post ID numbers are how WordPress identifies each piece of content on your website. Traditionally, the platform doesn’t display that information openly, but it’s easy enough to find if you know where to look for it. In fact, there are a lot of ways that you can get post IDs in WordPress.

In this article, we’ll show you five different ways to get post IDs in WordPress, ranging from the traditional to the unorthodox.

Let’s get to it!

Prefer to watch the video version?

Why It’s Useful to Know How to Identify WordPress Post and Page IDs

Internally, WordPress recognizes specific pages and posts by their ID numbers. That information is sometimes necessary if you’re using a plugin that asks you which posts you want to include or exclude from its effects.

WordPress post IDs are also necessary in some cases for building custom shortcodes. If you’re using a shortcode that requires you to specify a post, it will ask you to enter that post’s ID as a part of its parameters.

Another, more advanced example occurs if you’re adding custom code to your website, but you only want to target specific pages. In that scenario, you can tell WordPress that if a post or page matches the ID(s) you set, it should run the code that you want:

if(is_single(POST_ID))

Regardless of which scenario you’re facing, there are a lot of ways you can find out what any post or page’s ID is in WordPress.

Let’s talk about what they are.

How to Get Post IDs in WordPress (5 Methods)

Finding post IDs in WordPress is remarkably simple if you know where to look. Let’s start with the most straightforward approach, and then work our way down the list.

1. Find The ID Within Each Post’s URL

The easiest way to find a post ID in WordPress is to go to your dashboard and click on the Posts menu option. You should see a list of all the posts on your website, and finding their IDs is as easy as mousing over each title:

hovering over post to see its ID
Mousing over a post’s title to see its ID.

In the above example, the post ID is 1, and that number comes right after the posts= parameter.

If for some reason you can’t spot the ID clearly, you can open the post using the WordPress editor. That way, its URL will show up in your navigation bar, which should make the ID easier to spot:

find post by checking url
Finding a WordPress post’s ID by checking out its URL.

Keep in mind that the post URLs you see in your dashboard might be different from the ones your visitors encounter. That’s because a lot of WordPress websites use custom permalink structures that don’t show post IDs at all.

The only two permalink structures that do include WordPress post IDs are the Plain and Numeric options.

Here are two quick examples of what those URL structures look like:

  1. yourwebsite.com/?p=1
  2. yourwebsite.com/archives/1

In both cases, the post ID is 1. Although both URL structures aren’t necessarily bad, in most cases, you’ll want to use permalinks that give visitors an idea of what content they can expect from each page.

2. Use Custom Code to Display Post IDs in The Posts Tab

If you take a look at your Posts tab, you’ll notice that it includes a lot of information about each piece of content, including its author, tags, categories, and more:

wordpress posts tab
The WordPress Posts tab.

It’s possible to edit your theme’s functions.php file to add a new column to that table. This column will display each post’s ID, so you don’t have to dig into its URL to find that information.

You’ll want to use a File Transfer Protocol (FTP) client to modify that file. Access your website via FTP, open the WordPress root folder, and navigate to wp-content/themes. Find your theme’s folder inside (we hope you’re using a child theme!), and open the functions.php file located within.

Your FTP client will download the file and open it with your default editor. Here’s the code you’ll want to add to this file:

function add_column( $columns ){
	$columns['post_id_clmn'] = 'ID'; // $columns['Column ID'] = 'Column Title';
	return $columns;
}
add_filter('manage_posts_columns', 'add_column', 5);

function column_content( $column, $id ){
	if( $column === 'post_id_clmn')
		echo $id;
}
add_action('manage_posts_custom_column', 'column_content', 5, 2);

That code includes an extra column in your Posts table. Keep in mind that if you’re using a plugin that adds extra data to that same table, you might need to modify the position indicated in the above snippet. Our example adds the new column in the fifth position (hence the “5” you see above), but you can adjust the code accordingly if needed.

Once the snippet is in place, save your changes to functions.php, and your Posts tab should look like this now:

post id column
A column that displays post IDs.

From this point on, you’ll be able to view post IDs right from this table.

3. Use a Plugin to Display Post IDs in WordPress

If you don’t want to modify your theme’s functions.php file, you can use a plugin that does the dirty work for you. Our recommendation is Show IDs by 99 Robots:

The Show IDs by 99 Robots plugin
The Show IDs by 99 Robots plugin.

This particular plugin does exactly what we did during the last section, and it goes a bit further. On top of displaying IDs for your posts, it also includes that information for pages, categories, tags, media files, and more.

Once you activate the plugin, you’ll see a new ID column appear when you check out the full list of any of the elements we mentioned above. For example, here’s what our Pages tab looks like with the plugin set up:

pages tab with the show ids plugin activated
The Pages tab with the Show IDs by 99 Robots plugin activated.

Any time you need a post ID, all you need to do is go to the Posts page and copy it.

4. Find Post IDs Within the WordPress Database

As you may know, the WordPress database stores all of the information on your website, including the IDs for each individual post, page, and piece of content. Some web hosts, including us, enable you to access your site’s database using a custom interface.

If you’re a Kinsta user, you can access your database through MyKinsta, our custom dashboard:

accessing phpmyadmin via kinsta dashboard
Accessing phpMyAdmin via the Kinsta Dashboard.

We enable you to access your database using phpMyAdmin. Once inside, open your site’s database and move over to the wp_posts table. You should see each post’s ID inside under the ID column to the left of post_author:

post id database
Finding post IDs within your WordPress database.

As usual, all you have to do now is copy and paste the post or page ID you need, and you’re off to the races.

5. Use Functions to Fetch WordPress Post IDs

If you’re a developer, you might not need to look up WordPress post IDs at all. With your superpowers, you can use functions to fetch the IDs you need using the right parameters.

For example, you can use the get_the_id() function to return the ID of the post where it’s executed:

get_the_id();

If you want to have a little more fun, you can also fetch post IDs by their titles or slugs, although these two functions are a little less practical:

$mypost = get_page_by_title( 'Your post title goes here', '', 'post' );
$mypost->ID;
$mypost = get_page_by_path('post-slug', '', 'post');
$mypost->ID;

Another approach you can use is fetching a post ID from its URL, using the following function:

$mypost_id = url_to_postid( 'https://yourwebsite.com/your-post' );

If you want to find the post IDs within a WordPress loop, you can use the following code:

$id_query = new WP_Query( 'posts_per_page=6 );
 
while( $id_query-have_posts() ) : $id_query->the_post();
	$id_query->post->ID;
endwhile;

Using functions to fetch WordPress post IDs can come in handy if you’re adding custom functionality or building your own plugins. However, if you only need to find the IDs for a few specific posts, then you’re better off using one of the other approaches we discussed above.

Summary

Although WordPress doesn’t clerly display post or page IDs, there are so many ways to find them. Knowing what each post’s ID is can come in handy for enabling features within plugins, using shortcodes, and even your own development projects.

If you’re looking for a specific WordPress post ID, there are five ways that you can locate it:

  1. Find the ID within each post’s URL.
  2. Use custom code to display post IDs in the Posts tab.
  3. Use a plugin to display post IDs in WordPress.
  4. Find post IDs within the WordPress database.
  5. Use functions to fetch WordPress post IDs.





Source link

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?