Skip Navigation

[Resolved] Trying to set a last-modified date field for CPT post using PHP

This thread is resolved. Here is a description of the problem and solution.

Problem:
"save_post" action not working with Types

Solution:
To make "save_post" action working with Types you need to set priority 20 or later.

You can find proposed solution with the following reply:
=> https://toolset.com/forums/topic/trying-to-set-a-last-modified-date-field-for-cpt-post-using-php/#post-578459

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

This support ticket is created 7 years, 1 month ago. 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by Minesh 7 years ago.

Assisted by: Minesh.

Author
Posts
#578432

Tell us what you are trying to do?
Update a custom field for CPT when published/updated from Edit (custom post name) page.
Is there any documentation that you are following?
I can't find a way to do this in Types API.

I have a CPT "New Membership Forms" with a custom field, "nma-last-modified".
Here is the code I am trying to make work:

[code]
function cnsv_last_mod_date_timestamp($post_id, $post) {
$thePostType = get_post_type($post_id);
if ($thePostType == 'new-membership-form') {
$tz = date_default_timezone_set('America/Los_Angeles');
$nma_current_datetime = date("F j, Y, g:i A T");
update_post_meta($post_id, 'wpcf-nma-last-modified', $nma_current_datetime);
}
}
add_action('save_post', 'cnsv_last_mod_date_timestamp',10,2);
[/code]

Oops! I forgot how to mark code here. Sorry!

Thanks,
Jeff

#578459

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - I think you missed the following Doc where we explain with example how you can update Types custom field using PHP.

For example:

function is_project_overdue( $post_id, $post ){
    if ( 'project' == $post->post_type ) {
        $deadline = get_post_meta( $post_id, 'wpcf-deadline', true );
        $now = time();
        if ( !empty($deadline) && $now > $deadline ) {
            update_post_meta( $post_id, 'wpcf-overdue', 1 );
        }
    }
}
add_action( 'save_post', 'is_project_overdue', 20, 2 );

Please note that if you are using date field, Types date field store unix timestamp as its value So do not forget to convert your date to timestamp.

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

#579253

Minesh -

First of all, thank you! I believe (and hope) this documentation you posted here will help. I was never aware with this issue of using priority 20.

Second, I have a few issues with Toolset Support pages and I am not sure whom else I should forward these to.

1. I never received an email notification that you posted a reply to this issue of mine. This is the third time (in a year or so) this has occured. Yes, I checked my spam folder, etc. In fact, I have wp-types.com whitelisted on my mailserver, so it should always put them in my Inbox.

2. The indexing mechanism for Toolset documentation seems to have some problems. The last 3 ot of 5 issues I had with Toolset (in some manner) I was not able to find the answer in the documentation using Search. Then, Support Forum Moderator was able to show me the exact documentation I needed. Why don't the keywords find the correct documentation. I am using very obvious words for searching but, they don't find the docs I need. And sometimes the search comes back with nothing found, when those words are clearly included in the doc pages.

3a. The bottom of a post in Support where I am able to write a Reply is quite ambiguous. Sometimes it makes good sense to leave a final comment to the Forum Moderator but, it does not make sense to check the "I still need assistance" radio button.

3b. If I click "My issue is resolved" radio, I should be able to rate the Forum Moderator as "GOOD" without being forced to type a comment. This is quite annoying.

Thanks again, Minesh!
I am now going to try and change my priority to 20 and hope it works.

P.S. I clicked "I still need assistance" on purpose here, just to prove my point in 3a.

Jeff Safire
---------------

#579254

Minesh -

I actually found that I was using the wrong slug value. The name of the CPT was "CNSV Member Profile" but, the slug was set to "members". However, I am glad I found out about using priority 20 to avoid flakiness.

Thanks!
Jeff Safire

#579255

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

1. I never received an email notification that you posted a reply to this issue of mine. This is the third time (in a year or so) this has occured. Yes, I checked my spam folder, etc. In fact, I have wp-types.com whitelisted on my mailserver, so it should always put them in my Inbox.
==> Ok - I've marked this ticket for email diagnostic and if you still not able to receive the email. Please let me know and I will ask concern person from our team to look at this issue.

2. The indexing mechanism for Toolset documentation seems to have some problems. The last 3 ot of 5 issues I had with Toolset (in some manner) I was not able to find the answer in the documentation using Search. Then, Support Forum Moderator was able to show me the exact documentation I needed. Why don't the keywords find the correct documentation. I am using very obvious words for searching but, they don't find the docs I need. And sometimes the search comes back with nothing found, when those words are clearly included in the doc pages.
==> We are constantly working on our Doc and there are lot of changes still going on and work is in progress. Still, I will forward your concern to our systems team and will get back to you with updates on it if any.

3a. The bottom of a post in Support where I am able to write a Reply is quite ambiguous. Sometimes it makes good sense to leave a final comment to the Forum Moderator but, it does not make sense to check the "I still need assistance" radio button.
==> I will again share this concern to our systems team.

3b. If I click "My issue is resolved" radio, I should be able to rate the Forum Moderator as "GOOD" without being forced to type a comment. This is quite annoying.
==> I will again share this concern to our systems team.