Check wordpress posts and display expiration date

Hi, 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:

<?php $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

Thanks,
Charis

Well, your current code is simple to understand.

The question is how do you know which is published or scheduled. I would assume you have a field value
similar to your “_expiration_date”, something like “_scheduled_or_not” or whatever. You will need to know
what that field name is before you can compare it. Then, you can load a PHP variable with that value just
like you did the $expiration_date variable. Then, just compare it to echo what you need.

Not sure if this has helped. The first step it to find out how your system indicates scheduled or posted.

Thanks for your response. After some research and some help here’s what i came up with:

[php]<?php
$postid = 3823;
$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 ) );
?>

<?php $postid = 3823; $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 ) ); ?> <?php $postid = 3847; $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 ) ); ?>

[/php]

As you can see, since i would like to check for 2 posts and see which one is active, i am using the code twice (one for each post number). It does work but I feel i’m taking the long way here. is there a better way to do it?

Charis

Well, basically, you are pulling info from your Wordpress system using the get_post_meta. This just pulls
data and places it inside a variable. Therefore, you can use PHP to compare the information and only give
the display of which is the correct one to show.

I am not totally clear on what you are asking. What do you mean by seeing which of two posts are active?
If they are separate posts, then both can be active. Correct?

Also, the two posts that you coded for are the same ones. Both code segments are for post #3823.
So, with the same post numbers you will get the same results.

As you mentioned in the first post, you will have some posts marked as “published” and some “scheduled”.
Is the scheduled ones marked is the post_status? If so, your code should work as is. But, for multiple
posts, you would have to check all of the posts to see what ones are “published”.

Is this code for just two special posts out of all the posts? Not sure why you want to check for two
certain posts out of all of them?

Give us a little more info on what you want to accomplish… Thanks…

Apologies for both posts having the same number, they should have a different one. Let me explain again, perhaps my previous explanation wasn’t correct:

I would have 2 posts: Post-A will be published with an expiration date. Post-B will be inactive and scheduled to become active when post A expires. Post B will also have an expiration date. Therefore only one post will be active at all times.

Now, i would like to check both posts to determine which one is active and echo the expiration date of the active post. My code below actually works but my question is if my code is correct or i should do it with a different code.

Charis

Well, with that explanation, your code should word as-is. It does NOT check to see if the post is
active (published) or scheduled. It does check to see if either post is active. So, the code will work
perfectly if you are only dealing with those two posts. One will be positive and display and the other
will be negative and not display. Since it is only 10 lines of code, all is well. But…

Normally if there are two compares needed you would do it something like this:
if (condition1) {
do something…
}elseif (condition2) {
do something…
}

But, since we are talking about only two posts, it does not really matter. If there were many posts to
check, then, you would want to handle it with a different code layout.

Now with that said, if this code is inline, meaning if it is all one code segment, you would not need several of the lines. You only need to set the date format once and the extra <?PHP and ?> are not needed.
Lastly, if you rewrite the code as above, the second condition never happens if the first one is positive.
Therefore, it would cut down on processing, but we are only talking about 4 lines of code, so not really
an issue.

Hope all of this helps…

Yes, your information is very helpful actually. Let me thank you for the time and effort you have put in solving my issue!

Regards,
Charis

No problem. Glad I could help. And, thanks for the Karma !

Should we mark this one solved? Or, we can leave it open if you have further questions…

I have one more if you don’t mind. I will try to explain this as best is i can:

Check this page on my website: www.besmart.com.cy/offers/supermarkets/limassol/mas/

-The “View Offer” buttons has a link to a specific post (let’s say Post ID 1234).
-Underneath the button, there is a “Valid Until” field.

As you may have understood already, i am trying to get the expiration date of the post and then display it next to the “Valid Until” field automatically.

The code we have been discussing until now does that, only if we already know the Post ID. Would it be possible to actually get the post ID from that link and then display the posts expiration date?

I am not sure how it could be possible to grab the post ID automatically from the link behind the button.

Charis

Well, a quick look at the source of your page show that the “Valid Until” is inside this

area…

 Valid Until

So, looking at that it appears that you can place the the PHP code you posted into the just after the “Valid Until” text. This should display the expiration date there.

But, if the post number changes, you would have to handle that so the correct post number is pulled from
the PHP code.

Does that make sense?

One further thought…

Make sure you remember that PHP is SERVER-SIDE only. HTML and Javascript is CLIENT-SIDE only.
What this means is that ALL PHP is handled on the server and the output of it is sent to the browser.
At that point, the BROWSER takes control and used HTML, JS, JQuery and CSS to “RENDER” the results
to the screen for the user.

This means that you can use PHP to stick outputs pulled from the Wordpress system and display them
where you need them. Just remember PHP is SERVER only…

Thought I would explain that so you might understand the process better. Good luck!

It does but i think that is exactly my problem. I will have many Pages and many posts so i am trying to see if there is a way to pull the post ID automatically since it won’t be static and then use it to determine if the post is published (and therefore display the date).

As for how i am using the code, i am using a plugin that allows me to add PHP and use it in a shortcode (the page you are currently seeing is not ready yet, i am just using it as an example so you should understand what i am trying to achieve). I just prepared a correct page example for you to see:

http://staging.besmart.com.cy/offers/supermarkets/sample-offers/

Well, you understand how to check for the published option using a hard coded post ID number.

You just need to find out how and where your Wordpress pulls the post ID number when it looks up
the data to be displayed. Then, snag it and use it instead of setting the $postid variable. Most likely,
it would be something like…
$postid = get_post_id_number();

This function call depends on what Wordpress template you are using and how you are placing it on the
screen. Since you are using a template, there should be documentation on how that template displays
your posts. You actually should be able to look at the display page and see where it parses thru all of
the posts and displays them. Then, you should be able to see where it uses the post-ID to get the data
to display. That is where you would place your code to pull the expiration dates.

For some reason, the page you posted will not display for me. It comes up with a Wordpress login page.

Well, I have to leave for several hours soon. I will check in to see how you did on it when I get back.
Good luck!

Since i am using a plugin to expire my posts, this is the official documentation from the plugin on how to get the post ID and then echo it somewhere:

[php]<?php
$date_format = __( ‘M j, Y @ G:i’ );
$expiration_date = get_post_meta( get_the_ID(), ‘_expiration_date’, true);
echo 'This post will expire on ’ . date_i18n( $date_format, strtotime( $expiration_date ) );
?>[/php]

Now this works just fine as long as i place this code inside the post. What i have now is:

A page —> a button with a link in that page ----> the link goes to a post.

How can i place a code in the page, that will automatically discover the post behind the link, get the post ID and then display it’s expiration date on that page? This is the part that i am missing. Hope my explanation isn’t confusing.

Apologies about the non-working page, it should be fine now. Also i would like to remind you that your help is highly appreciated :slight_smile:

Charis

Just to let you know that i used custom fields to solve my issue :slight_smile: you may go ahead and close the topic. Again, big thanks for your great support!

Charis, Glad you solved your issues. I was not home for a couple days and just checked in on you.

I will mark your topic solved.

Sponsor our Newsletter | Privacy Policy | Terms of Service