Captain Comic
Captain Comic

Reputation: 16196

Getting postid from permalink

How can I get post id from the url. For instance I have opened the page mysiteurl/mypagename in the page handler i need to get post id.

If I had default permalinks than I could get from the name /?p=123 but I have non-default ones so I cannot get post ID from the name

Something like UNPERMALINK because it does the reverse as the_permalink.

NB - I dont have the loop.

Upvotes: 0

Views: 2130

Answers (1)

themerlinproject
themerlinproject

Reputation: 3582

Assuming you have the wordpress framework loaded you can use url_to_postid() - the opposite of get_permalink():

<?php $postid = url_to_postid( $url ); ?> 

with your permalink as $url

Upvotes: 4

Related Questions