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?
-
<?php global $post; echo $post->ID; ?>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.
well how are you determining the permalink?
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.
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.
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.
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.
so, if I store the post ID instead, how easy is it to determine the permalink?
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.
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.
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.
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://ww.wp.xz.cn/extend/plugins/reveal-ids-for-wp-admin-25/
Works fine in 2.7.
The topic ‘using PHP to determine post ID from permalink’ is closed to new replies.