Skip Navigation

[Resolved] Calculating custom field value and storing it with post add/edit

This support ticket is created 5 years, 5 months 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
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)

Tagged: 

This topic contains 8 replies, has 2 voices.

Last updated by Christian Cox 5 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1376307

The need: admin panel to sort on a single metadata field
The issue: Custom post type has two metadata fields: event date, event time
The request:
a) create a custom field in Types, then on Add or Edit of the "event CPT", have the custom field automatically calculate as a concatenation of the event date & event time, then store it's value as a new metadata field with the CPT.

b) set the new "date/time" custom field to "calculated" or "read only"... or even "hide" it on the CPT for the event.

Ideas?

#1376357

If you plan to create and make changes to these posts in wp-admin, then you can use the save_post hook:
https://developer.wordpress.org/reference/hooks/save_post/

Inside that save_post hook you can access Types custom field values using get_post_meta:

get_post_meta( $post_id, 'wpcf-slug', true);

You could also set Types custom field values using update_post_meta:

update_post_meta( $post_id, 'wpcf-slug', 1572965318);

The type of custom field dictates the data format. In the previous update_post_meta example, I stored a Unix timestamp for a Types custom date field. Other date fields store information in other formats, but Unix timestamp is easy to work with. Not sure what advice you're looking for here, so if you have specific questions please let me know.

#1376395

Hi Christian,

I am looking for:
1) enhancement request for Toolset Types: add "calculated metadata field" to the metadata fields component, then let us specify a calculation, the result of which is "automatically" calculated and stored with the metadata... This would be having my cake and eating it too, where Toolset Types handles all the code / execution, etc..

But, short of that (because I don't think it will happen any time soon).. Looking for a full code snippet that triggers on "save_post_session" action (session is my custom post type). I have figured out what you've already shared, BUT what I am wondering now is:

Is the metadata array already a part of the POST data passed in to the save action? Or do I really have to "get the metadata" (not a problem), by using get_post_meta??

I am also wondering: update_post_meta - ok, but will this action work inside of the SAVE action hook? or will the update I do, get overwritten by the rest of the "save hook code"?

Thanks,
Dan

#1376405

1) enhancement request for Toolset Types: add "calculated metadata field" to the metadata fields component, then let us specify a calculation, the result of which is "automatically" calculated and stored with the metadata
Feel free to submit your request here: https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/
It most likely will not be implemented right away, as you said.

Looking for a full code snippet that triggers on "save_post_session" action (session is my custom post type).

add_action( 'save_post', 'auto_number', 100, 3 );
function auto_number( $post_id, $post, $update ) {
  if ( $post->post_status == 'publish' && $post->post_type == 'session' ) {
    update_post_meta( $post_id, 'wpcf-slug', 12345 );
  }
}

Here is a full code snippet triggered on save post, limited to published session posts. There are known issues with save_post_session timing when editing posts with Forms, so we do not recommend that action hook.

Is the metadata array already a part of the POST data passed in to the save action? Or do I really have to "get the metadata" (not a problem), by using get_post_meta??
It depends on how the post is being saved. I think it's best to call get_post_meta in case you're editing the post programmatically, or through Forms, or some other method that doesn't include the full post metadata in each edit.

I am also wondering: update_post_meta - ok, but will this action work inside of the SAVE action hook? or will the update I do, get overwritten by the rest of the "save hook code"?
Yes, this action will be triggered after saving or updating the post. The update_post_meta function will work even if no value currently exists for the custom field.
http://codex.wordpress.org/Function_Reference/update_post_meta

#1376441

I'm very close. First, thank you Christian your support is awesome, and your response timeliness is great! 🙂 I have the code:

add_action( 'save_post_session', 'wwdvcSession_save',100,3 );
function wwdvcSession_save($id, $post, $isupdate) {
if ($post->post_status == 'publish' && post_type =='session') {
$sess_date = date("Y-m-d",get_post_meta( $id, 'session_date', true ));
$sess_time = get_post_meta( $id, 'session_time', true );
$sess_dt = strtotime($sess_date.' '.$sess_time);
update_post_meta($id, 'wpcf-session_datetime',1589760000);
}
}

I realize I have a hard-coded value for wpcf-session_datetime, I have done this for "testing" purposes.

2 things are happening:
1) The hard coded value / update is completely ignored (doesn't change the value in the database)
2) when I click "clear" inside the edit post UI, the metadata field is deleted / disappears from the wp_postmeta table.

What am I doing wrong?

#1376447

Don't use save_post_session. There are known timing issues with save_post_session overwriting custom field values. You should use save_post instead, and rely on the conditional that is already in place to apply the code only to published session posts:

if ($post->post_status == 'publish' && $post->post_type =='session') {
#1376453

Error in the code line:
if ($post->post_status == 'publish' && post_type =='session') {

Fixed to:
if ($post->post_status == 'publish' && $post->post_type =='session') {

But changing your action trigger to SAVE_POST works. Genius, thank you! all good now. Please mark this solved.

#1376455

My issue is resolved now. Thank you!

#1379519

Updated my code, thanks for the catch.