I will try to explain my issue as good as i can. Please take a look at the following page in my website.

We have:

  • The main page
  • Inside the main page there is a "view offer" button
  • The "view offer" button links to a post.
  • The post has an expiration date (done through a plugin)
  • Underneath the "view offer" button there is a "valid until" field

What i am trying to achieve is the following:

I would like to get the post ID behind the link automatically, use the post ID to get the post's expiration date and then display it next to the "valid until" field automatically. I can already achieve this but i would like to be a bit more automated. Here's the code i am using so far:

$postid = 4407;
$date_format = __( 'd / m / Y' );
$expiration_date = get_post_meta( $postid, '_expiration_date', true);
if (get_post_status($postid)=='publish')
echo date_i18n( $date_format, strtotime( $expiration_date ) ); 

My problem with the above code is that it's only valid if i already know the Post ID number and since i have many pages and posts, i would have to create different code for each page and post. I would like to create a single code that could apply to all of my pages. So basically, i would like to grab the post ID automatically based on the link behind the "view offer" button.

As for how i am using PHP, i have a plugin that allows me to create PHP code snippets and then use them anywhere i like as shortcodes. Therefore, i have a shortcode with the PHP code above next to "valid until" field.

I'm not sure that is possible or if my approach to begin with is wrong but any help would be highly appreciated.

Regards, Charis


1 Answer 1


I guess you're not after the current post/page ID, but rather trying to find the ID for some url from your site.

Then you should try out the handy url_to_postid() WordPress core function.

It returns the post/page ID if it exists, else 0.


echo url_to_postid( 'http://example.com/2014/11/14/hello-world' );

// Outputs the post ID as 1 in this example.


Reply to the comments: You could create a custom field, called e.g. url_offer, from the backend

Example for a custom field

and use:

$url_offer = get_post_meta( get_the_ID() , 'url_offer', true );

to retrieve it on the front end.

  • Thinking about it, you actually might have understood the question correctly here. I was not really sure, so I posted my understanding as a comment :-). Can see the Friday feeling has already kicked in. Nov 14, 2014 at 10:30
  • Thank you both for your replies! Yes, birgire is correct on what i'm trying to achieve. url_to_postid() seems useful but again, i would have to state the URL to the post. I am trying to get that automatically without needing to input any information like Post ID or URL Nov 14, 2014 at 10:41
  • I am running an offers website with lots of pages and posts. If it helps, each page will always be related to a specific post. Nov 14, 2014 at 10:43
  • @PieterGoosen Let's see what happens, wouldn't be the first time I misunderstood things ;-) ps: here's a suggestion for the song of the day, some Friday feeling ;-)
    – birgire
    Nov 14, 2014 at 10:47
  • @thundertrax ok great, you could for example store the url or post id in a custom field?
    – birgire
    Nov 14, 2014 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.