Title: using PHP to determine post ID from permalink
Last modified: August 19, 2016

---

# using PHP to determine post ID from permalink

 *  Resolved [myinstinctwaswrong](https://wordpress.org/support/users/myinstinctwaswrong/)
 * (@myinstinctwaswrong)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/using-php-to-determine-post-id-from-permalink/)
 * I have a need to detemine the post ID from within PHP – all I know is the permalink
   of the post. Can anyone point me to a source for ways to do this?

Viewing 12 replies - 1 through 12 (of 12 total)

 *  [whooami](https://wordpress.org/support/users/whooami/)
 * (@whooami)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/using-php-to-determine-post-id-from-permalink/#post-931040)
 * `<?php global $post; echo $post->ID; ?>`
 *  Thread Starter [myinstinctwaswrong](https://wordpress.org/support/users/myinstinctwaswrong/)
 * (@myinstinctwaswrong)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/using-php-to-determine-post-id-from-permalink/#post-931050)
 * sorry, I didn’t explain myself well enough – I am writing a script that accesses
   a different sql db and creates a text file list of post IDs – this is being used
   for a “featured” page.
 * when I access the other db – I can determine the permalink of a post – so I need
   to take that and query wp and get the post ID.
 *  [whooami](https://wordpress.org/support/users/whooami/)
 * (@whooami)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/using-php-to-determine-post-id-from-permalink/#post-931052)
 * well how are you determining the permalink?
 *  Thread Starter [myinstinctwaswrong](https://wordpress.org/support/users/myinstinctwaswrong/)
 * (@myinstinctwaswrong)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/using-php-to-determine-post-id-from-permalink/#post-931056)
 * that is actually stored in the other db as part of the record setup. Looking 
   at the wp tables using phpmyadmin, I don’t see any place that the permalink is
   actually stored so I can’t query on it. Looks like I need to revamp something.
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [17 years, 5 months ago](https://wordpress.org/support/topic/using-php-to-determine-post-id-from-permalink/#post-931057)
 * > _when I access the other db – I can determine the permalink of a post – so 
   > I need to take that and query wp and get the post ID. _
 * There is no simple way to do it. WordPress has lots of code just for this purpose.
   The parse_request function in the WP class in classes.php does the grunt work,
   and uses the WP_Rewrite object for permalink matching.
 *  [whooami](https://wordpress.org/support/users/whooami/)
 * (@whooami)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/using-php-to-determine-post-id-from-permalink/#post-931058)
 * its easier to get the post_id than the permalink. what is responsible for getting
   the permalink, and at what point? are you scraping the site, grabbing it when
   there’s a new post made.. what? If you are using a query to insert the permalink
   into the other db, get the post_id at the same time. Rather than going back at
   a later time to do so.
 *  Thread Starter [myinstinctwaswrong](https://wordpress.org/support/users/myinstinctwaswrong/)
 * (@myinstinctwaswrong)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/using-php-to-determine-post-id-from-permalink/#post-931063)
 * thanks for the responses. the other db is a list of applications we sell for 
   phones – each member creates the other db record and is responsible for their
   own WP post – WP is doing all the work for displaying the site – the backend 
   db and code is used to handle interaction with the user phone, paypal, etc.
 * The featured app page uses an array of postIDs to build itself. I don’t want 
   the WP front end having access to the backend dbs – so I use a cron job to generate
   that postid array and post it to a text file, and the WP page just includes and
   uses that.
 * Let me work on it – I need to change something in how we do things – that’s the
   biggest trouble with designing on the fly, lol.
 *  Thread Starter [myinstinctwaswrong](https://wordpress.org/support/users/myinstinctwaswrong/)
 * (@myinstinctwaswrong)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/using-php-to-determine-post-id-from-permalink/#post-931070)
 * so, if I store the post ID instead, how easy is it to determine the permalink?
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [17 years, 5 months ago](https://wordpress.org/support/topic/using-php-to-determine-post-id-from-permalink/#post-931072)
 * > _so, if I store the post ID instead, how easy is it to determine the permalink?_
 * Trivial. While in WordPress, you can do `get_permalink(POST_ID_NUMBER);` to get
   the permalink for a given post ID. Perhaps you can use this to build a list of
   permalinks and IDs that will let you match on in your application.
 * Kinda fragile, but it’d probably work.
 *  Thread Starter [myinstinctwaswrong](https://wordpress.org/support/users/myinstinctwaswrong/)
 * (@myinstinctwaswrong)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/using-php-to-determine-post-id-from-permalink/#post-931078)
 * ok, thanks. I think I’ll use that technique to find and then change the other
   db to store the post ID instead of the permalink – and then mod a different script
   that uses the permalink to look that up first using the post id.
 * The problem with running a backend behind WP is connecting the data between the
   two. In our situation, none of the fields are exactly the same – WP post title
   tends to be longer than the one used in the backend, etc. I needed something 
   early on to connect the two datasets – and at that time, I needed to create a
   link to the post, so I chose to use the permalink. Current needs are finding 
   that was a bad choice.
 * I appreciate your help.
 *  Thread Starter [myinstinctwaswrong](https://wordpress.org/support/users/myinstinctwaswrong/)
 * (@myinstinctwaswrong)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/using-php-to-determine-post-id-from-permalink/#post-931185)
 * the only trouble with this concept of storing the post ID is that the post ID
   is not an obvious piece of information to find. It is not displayed any where
   on the edit post screen.
 * The only place I’ve been able to get it is from the browser’s status bar when
   I hover over the link to edit a post.
 * Would be nice if it was more prominent – like under the Permalink line, add another
   line that said “Post ID: xxx” or some such – maybe over in the box with the publish
   date.
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [17 years, 5 months ago](https://wordpress.org/support/topic/using-php-to-determine-post-id-from-permalink/#post-931190)
 * > _the only trouble with this concept of storing the post ID is that the post
   > ID is not an obvious piece of information to find. It is not displayed any 
   > where on the edit post screen._
 * I know. I’ve complained about it too. Complaints will continue until they add
   it back. 🙂
 * Meanwhile: [http://wordpress.org/extend/plugins/reveal-ids-for-wp-admin-25/](http://wordpress.org/extend/plugins/reveal-ids-for-wp-admin-25/)
 * Works fine in 2.7.

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘using PHP to determine post ID from permalink’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 12 replies
 * 3 participants
 * Last reply from: [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * Last activity: [17 years, 5 months ago](https://wordpress.org/support/topic/using-php-to-determine-post-id-from-permalink/#post-931190)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
