WordPress assigns ID numbers to every post and page you publish. You can never change the ID, though you can change the titles and sub-directory or "slug" names. If you use the default URL structure for WordPress, you will see the ID of a post in the address bar of your browser when you visit that post. Most blogs use "Permalinks" that hide the post ID, so you need a trick to find that number. Programmers also need the ID for some template file code.
Find the ID in the Dashboard
Log into the WordPress dashboard by visiting this page:
Type in your user name and password, if prompted, and then press "Enter" to log in. The dashboard is the control panel for your WordPress website.
Click "Posts" in the left-hand menu and then click the post you want to check. Look at the address bar of your browser. The address contains a URL for "post.php" plus a question mark and "post=" with a number following that. The number is the ID for that post. While you can change the post title or the "slug," the ID always remains the same.
Click "Pages" in the left-hand menu and then click the title of a page. Look at the address bar again. Like with posts, pages also get post ID numbers. In the case of IDs, WordPress treats pages and posts the same way.
Find the ID Programmatically
Navigate to the folder of your current WordPress theme and open any template files you wish to edit using a code editor like Notepad++, jEdit or BBEdit. You can also edit from the dashboard by navigating to "Appearance" and then "Editor." Files are listed on the far-right side.
Use the get_the_ID() function to find the ID of a post or page. This function returns the ID without displaying it on a Web page; use the_ID() to display the post ID as content on the post or page. Assign the ID to a variable so you can pass it to other functions. Here is an example:
$post_id = get_the_ID();
Get the post ID by its title using a MySQL query. This is more complicated because WordPress does not offer a template tag function to do all the technical work for you. Here is some sample code:
$title = 'Sample Post';
$titleid = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '" . $title . "'");
The above code sets a variable to "Sample Post" that is then passed into a MySQL query. The query itself is saved to the "titleid" variable. You can then pass that variable somewhere else in the code or output it using "<?php echo $titleid; ?>".
Tips & Warnings
- When using code to find an ID, you should output the ID to the screen during the development phase so you can check you are getting the correct value. Use comment marks to "comment out" testing code before the website goes live.
- Back up your theme files before editing them.
- Photo Credit Jupiterimages/Photos.com/Getty Images