How to Find ID of Page, Post, Category, and Tag in WordPress
In some situation, such as searching articles in the database or create a shortcode to load javasctipt or css file, we need to know the ID of a page, post, category, tag, etc.. therefore, on this article we’ll discuss about how to find id of page, post, category, and tag in wordpress.
1. Find ID of Page or Post In Wordpress
Finding the ID of a post or page can be done manually or using a plugin, to do it manually, login to the administrator page and click the Posts or Pages menu, then point your cursor to the title of the article and see the links on the lower left corner.
From the figure above, we know that the ID is 825. If we were editing a post or page, then the ID of the article can be seen on the url in the address bar of the browser.
Same as the previous figure, the ID of the post in the figure above is also 825. If we were writing a new post or page, then the ID can be seen by hovering the permalink.
In the figure above, the ID of the new post is 93. now, If we only want to see the ID one or two times, it’s ok to use the above methods, but if we do it again and again, of course, it is not practical, so.. the solution is to add a column to the list of articles to display the ID, it can be done manually by writing some code or simply using a plugin. For example, in this tutorial we use the Catch ID plugin, and the result is:
There are several plugin that can do that task, some of them are: Show IDs by 99 Robots and Reveal IDs. Show IDs is similar to Catch IDs, both size are only about 4KB and just display the ID, no more option, why I choose to use Catch IDs is because I think the source code is neat, easy to read and understand, if you want more settings, such as only certain user that could see the ID, you can use the Reveal IDs plugin, but, notice that the size is about 200KB for just do the simple task.
Manually add the id column
For the manual method, add the following script in the functions.php file that located in the themes folder that we use.
For Post:
// Add column
function add_posts_id_column($col) {
return array_merge( $col, array('post_id' => 'ID') );
}
add_filter('manage_posts_columns' , 'add_posts_id_column');
// The content of column
function fill_posts_id_columns($column_name, $id) {
echo (int)$id;
}
add_action('manage_posts_custom_column', 'fill_posts_id_columns', 4, 2);
// Specify column width using inline css
function wdc_print_css()
{
echo
'<style type="text/css">
#post_id { width: 50px };
</style>';
}
add_action('admin_head', 'wdc_print_css');
The above code works only for post, for pages, simply change the above code to:
- Line 5: change the first argument of add_filter function from manage_posts_columns to manage_pages_columns
add_filter('manage_pages_columns' , 'add_posts_id_column')
. - Line 11: change the first argument of add_filter_function from manage_posts_custom_colum to manage_pages_custom_column
add_action('manage_pages_custom_column', 'fill_posts_id_columns', 4, 2)
.
2. Find ID of Category in WordPress
To find out the category ID, click on the Post » Categories menu, then on the list of categories that appears, point your mouse to one of the names of category, see url address that appears on the bottom left corner.
If you are editing the category (eg: after clicking the category name), the category id can be found on the url in the address bar of the browser.
If you think that the category id is important and needs to be seen any times, you can use a plugin Catch ID -like the previous solution-, the plugin will add an ID column to the list of categories in the category pages.
Manually add the category id column
If you just want to display ID only for category, maybe you don’t need to install a plugin, you can do it manually by adding some php function to your functions.php file.
// Show the column header ID
function wdc_add_category_id_column($col) {
return array_merge( $col, array('cat_id' => 'ID') );
}
// Show the category ID
function wdc_fill_category_id_column($val, $col_id, $id) {
if ($col_id == 'cat_id') {
echo (int) $id;
}
}
// Styling column width
function wdc_print_css()
{
echo
'<style type="text/css">
#cat_id { width: 50px };
</style>';
}
add_action('manage_edit-category_columns', 'wdc_add_category_id_column');
add_filter('manage_category_custom_column', 'wdc_fill_category_id_column', 10, 3);
add_action('admin_head', 'wdc_print_css');
3. Find ID of Tag In WordPress
To find out the Tag ID, first, go to the Tag page by clicking the tag menu (Post » Tags). In the list of available tags, hover your mouse over one of the tag name and see the url that appears on the bottom left corner.
In the above example, the tag ID is 2. If you’re already on the edit tags page (eg: after clicking on the tag name), the ID can be seen through the existing url in the address bar of the browser.
In order ID tag always comes up, you can use the Catch ID plugin like the previous solution, the plugin will add a column that contains tag ID to the list of available tag on tags page.
Manually add the tag id column
Again, If you just want to display ID only for tags, maybe you don’t need to install a plugin, simply add some php function to your functions.php file.
/* Show ID for default wordpress post tag */
// Show the column header ID
function wdc_add_post_tag_id_column($col) {
return array_merge( $col, array('tag_id' => 'ID') );
}
// Show the Tags ID
function wdc_fill_post_tag_id_column($val, $col_id, $id) {
if ($col_id == 'tag_id') {
echo (int) $id;
}
}
// Styling column width
function wdc_print_css()
{
echo
'<style type="text/css">
#tag_id { width: 50px };
</style>';
}
add_action('manage_edit-post_tag_columns', 'wdc_add_post_tag_id_column');
add_filter('manage_post_tag_custom_column', 'wdc_fill_post_tag_id_column', 10, 3);
add_action('admin_head', 'wdc_print_css');
4. Playing with plugins
Previously mentioned that there are several plugins that can be used to display the ID in various admin pages such as post, page, category, and tags. Actually, this plugin can display the ID for almost all pages that have ID, including gallery page (Showing the ID of the image) and user page. To play with these plugins, go to Add New Plugins page (Plugins » Add new) and type “show ID” keyword without quote, as shown in the following figure.
5. Conclusion
There are multiple ways for displaying ID data in WordPress, either manually or using a plugin. The benefit of the manual way is only add a simple code to our functions.php file, rather than push wordpress to read a bunch of code, but using a plugin is much easier and handy.
Subscibe Now
Loves articles on webdevzoom.com? join our newsletter to get quality article right to your inbox. Nothing else, just quality stuff!!!