Navigation überspringen

[Gelöst] Calculating post(s) age

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem: I would like to calculate the age of a post, in number of days, based on its publish date, and display that number for each post in a View of posts.

Solution: It would require a custom shortcode to calculate the difference between today's date and the post's publish date and display that in a View, something like this:

add_shortcode( 'num-days', 'wpv_num_days_shortcode');
function wpv_num_days_shortcode( $atts ) {
  $atts = shortcode_atts( array(
      'postid' => ''
  ), $atts );
  $timestamp = get_the_date( 'U', $atts['postid'] );
  if( $timestamp != '') {
    $age = floor((time() - $timestamp) / ( 60*60*24 )) ;
    return $age;
  }else {
    return 0;
  }
}

Then in the front-end the shortcode is like this:

[num-days postid="[wpv-post-id]"]
This support ticket is created vor 5 Jahren. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Dieses Thema enthält 22 Antworten, hat 3 Stimmen.

Zuletzt aktualisiert von Christian Cox vor 4 Jahren, 12 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#1400977

What I would like to do is create a view that can calculate the age of all 'active' items.

I have a custom post type called 'Defects' within defects I have a field called 'Status' so once a defect post is published, and if it stays in the 'active' status I'd like to show how long (in days for example) it has been active.

Thanks!

#1401105

Hello, there's nothing built in to Toolset that will track the date and time when some custom field was last changed. Instead, the simplest way to handle this is to create a custom date field, and manually set that date field when the post becomes 'Active'. If the post becomes "Inactive", then you would delete the custom field date. To display the number of days since that custom field date, you would use a custom shortcode like this:

add_shortcode( 'num-days', 'wpv_num_days_shortcode');
function wpv_num_days_shortcode( $atts ) {
  $atts = shortcode_atts( array(
      'postid' => '',
      'slug' => ''
  ), $atts );
  $timestamp = get_post_meta( $atts['postid'], $atts['slug'], true );
  if( $timestamp != '') {
    $age = floor((time() - $timestamp) / ( 60*60*24 )) ;
    return $age;
  }else {
    return 0;
  }
}

Then you can display the number of days like this:

Post has been active [num-days postid="[wpv-post-id]" slug="wpcf-datefieldslug"] days.

Replace datefieldslug with the slug of your date custom field.

#1401211

So does this happen automatically when the post is publised as active? Or does it require some manual intervention by the user?

#1402163

The simplest solution requires manual intervention by the User. A custom solution with PHP could be implemented to automate the field value based on the custom status field and/or the post status. The save_post hook can be used to trigger code whenever a post is saved or updated:
https://developer.wordpress.org/reference/hooks/save_post/

Toolset Types custom date fields store information in the database using Unix timestamp format, so you would update a custom date field value to today's date with code like this:

update_post_meta( 12345, 'wpcf-datefieldslug', time() );

https://developer.wordpress.org/reference/functions/update_post_meta/
https://toolset.com/documentation/customizing-sites-using-php/functions/

#1403225
Capture.PNG

So I went back to the original suggestion and added the function and shortcode and got :

(see screen shot attached)

Then it occured to me that in the function it asks for post id and slug. But if I specify a single post id it won't calculate for each individual defect. So do I need the view id? Which slug do I need to specify here?

#1404809

Then it occured to me that in the function it asks for post id and slug. But if I specify a single post id it won't calculate for each individual defect.
The num-days shortcode expects you to pass in as attributes:
- the slug of some date field
- the ID of whatever post contains the date field
I assume in your case the defect post holds the date field. When you place the num-days shortcode in a View of Defects, you should not hard-code a single post ID in its attributes. You should use the wpv-post-id shortcode so that the postid is dynamic for each iteration of the loop:

Active [num-days postid="[wpv-post-id]" slug="wpcf-datefieldslug"] days.

If the results aren't exactly what you'd expect, please copy + paste in your next reply the complete shortcode as you have it written in the View. Tell me the ID of one of the Defect posts, and tell me the slug of the custom date field.

#1404819

Christian, I think it might be easier for you to login and see it rather than for me to put all that here is public.

#1407097

Okay please provide login credentials here in the private reply fields and let me know where I can see the View with the age calculation.

#1412455

Did you create the custom date field which will store the date the post became "Active"? I don't see any date fields associated with the Defects post type, so...there's nothing to calculate.

Let me step back a bit. You must create a custom date field for the Defects post type, and assign the active date to each Defect post manually for now. Whatever slug you give that date field, you need to copy and update the code in the View's Loop template:
versteckter Link

[num-days postid="[wpv-post-id]" slug="wpcf-datefieldslug"]

Replace datefieldslug with the new field slug. The wpcf- prefix is required, even though you don't see it in wp-admin. In other words, if your date field slug in wp-admin is active-date then your shortcode should be updated as such:

[num-days postid="[wpv-post-id]" slug="wpcf-active-date"]

That should be enough to test with and confirm the calculation is accurate.

#1413371

I just want to calculate it against the date it was published. Why do I need a custom field for this?

#1413467

I just want to calculate it against the date it was published. Why do I need a custom field for this?
As I understood the first comment in this ticket, there is a custom field "Status" that must also be considered. If that is not the case, then you are correct and no date custom field is required. However, if the Status field value must also be considered when calculating the age, then you need a way to record when Status changes occurred, because those changes would not affect the publish date of the post but would affect the age calculation. As a simple solution, I suggested using a custom date field to manually set the start date of the age calculation.

Did I misunderstand the requirement for the Status field, or is that no longer important in determining age?

#1413773

No, I think you understood. But I don't think it really matters all that much since the active/inactive items will be filtered (overall) in the views.

#1414873

Okay so something like this in the custom code:

add_shortcode( 'num-days', 'wpv_num_days_shortcode');
function wpv_num_days_shortcode( $atts ) {
  $atts = shortcode_atts( array(
      'postid' => ''
  ), $atts );
  $timestamp = get_the_date( 'U', $atts['postid'] );
  if( $timestamp != '') {
    $age = floor((time() - $timestamp) / ( 60*60*24 )) ;
    return $age;
  }else {
    return 0;
  }
}

Then in the front-end the shortcode is like this:

[num-days postid="[wpv-post-id]"]
#1415055

It only works on the 'my defects' tab. I put the same code in each view ( most of the other tabs) and it doesn't work. What am I doing wrong?

#1415475

I checked the "Open" tab, and it seems that the View shown there is this one:
versteckter Link

Look carefully, and you will see the number of days shortcode isn't included anywhere in this View. While the shortcode is included in the loop template editor below the loop editor, the template shown below the loop editor isn't actually included in the loop! That's tricky! The template included in the loop is actually this Content Template:

[wpv-post-body view_template="loop-item-in-defects-dashboard-all"]

So the loop template that was created in another View has been placed here manually. Next I checked the "All" tab and I can see this is the View:
versteckter Link

This is where the loop-item-in-defects-dashboard-all template was created, so you can edit it here or you can edit it in the main Content Template editor here:
versteckter Link

You would have to add the shortcode in this Content Template for it to appear in the open tab, or place a different loop template in Loop Editor of the Defects Dashboard - Open View here:
versteckter Link

Make sense? I didn't go through the other tabs, so if there are other issues to address after you make this change let me know.