I'm a beginner in PHP and Wordpress and I would appreciate help on project I'm working on.

Currently I am using the below code to query a specific post and then echo it's expiration date (expiration date is set through a plugin) in a wordpress site:

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

This is working fine. What I would like it to do is:

  • I will have 2 posts, 1 published and one scheduled
  • Query both posts, check which is publiched and echo it's expiration date
  • Could you add "the below code"? :) – Jimtrim Nov 11 '14 at 14:12
  • Welcome to WPSE. Just a tip, when adding code, highlight it and click on the {} sign above the editor. This will add your code in a code block that will make your code more readable :-). To learn how the site operates, feel free to take a tour and visit How to Ask and help center to learn how to ask good questions – Pieter Goosen Nov 11 '14 at 14:21

I think that you are looking for the function get_post_status Example:

if (get_post_status($postid)=='publish')
echo date_i18n( $date_format, strtotime( $expiration_date ) ); 
  • Thanks for your reply! The function you supplied did the trick but i think my code is taking the long way. I am using the above code - including your IF statement - twice, one for each post number. Is there a quicker way? – thundertrax Nov 11 '14 at 17:04
  • Yes. Ideally you would use the loop (codex.wordpress.org/The_Loop) to read through your posts. You could also use query_post to apply a specific category to your two posts and read only the posts using that category (codex.wordpress.org/Function_Reference/query_posts). Scroll down that first link for an example checking the category. – Nomad Coder Nov 12 '14 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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