using the guide here https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/
In an attempt to automatically covert the performance time to seconds I created a custom field similar to your previous example and tried to use the below code to update the field but it does not seem to work
function change_to_seconds( $post_id, $post ){
if ( 'swimming-event-player' == $post->post_type ) {
$timestring = get_post_meta( $post_id, 'wpcf-swimming-performance', true );
if (strlen($timestring) == 5) {
$fulltime = "00:" . $timestring;
list($hours,$mins,$secs) = explode(':',$fulltime);
$seconds = mktime($hours,$mins,$secs) - mktime(0,0,0);
update_post_meta( $post_id, 'wpcf-swimming-performance-seconds', $seconds);
} else {
list($hours,$mins,$secs) = explode(':',$timestring);
$seconds = mktime($hours,$mins,$secs) - mktime(0,0,0);
update_post_meta( $post_id, 'wpcf-swimming-performance-seconds', $seconds);
}
}
}
add_action( 'save_post', 'change_to_seconds', 30, 2 );
You're asking me to troubleshoot and debug custom code. I can show you how to set a custom field value using PHP, but beyond that custom code like this is outside the scope of support we provide here in the forums. Please refer to our support policy: https://toolset.com/toolset-support-policy/
If you want to debug your own custom code, I suggest you add error log statements like so:
function change_to_seconds( $post_id, $post ){
error_log('change_to_seconds');
if ( 'swimming-event-player' == $post->post_type ) {
error_log('swimming-event-player post type');
$timestring = get_post_meta( $post_id, 'wpcf-swimming-performance', true );
error_log('timestring: ' . $timestring);
...and so on, for each step of the code. Then you will be able to watch the server logs to determine why the code isn't working as you might expect. If you're not familiar with server logs, I can show you how to activate them temporarily. Go in your wp-config.php file and look for
define('WP_DEBUG', false);
Change it to:
define('WP_DEBUG', true);
Then add these lines, just after the WP_DEBUG line:
define('WP_DEBUG_LOG', dirname(__FILE__) . '/error_log.txt');
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
define('WP_DISABLE_FATAL_ERROR_HANDLER',true);
Then trigger the save_post event. This will create an error_log.txt file in your site's root directory, so you can debug each step of the code.
Okay, I understand. What I really want to ask is if there is any way I can automatically copy information from a field in an intermediary post to another field in the same intermediary post, similar to the way you showed me in copying from parent to child
Yes you can do something like this to copy from one field in an intermediary post to another field in the same intermediary post:
$post_id = 12345;
$value_to_copy = get_post_meta( $post_id, 'wpcf-field-1', true);
update_post_meta($post_id, 'wpcf-field-2', $value_to_copy);
Replace 12345 with the ID of the intermediary post, replace field-1 with the slug of the field you want to copy from, and replace field-2 with the slug of the field you want to copy to.
Adding this to the initial sample demo does not seem to work
add_action( 'toolset_association_created', 'automate_intermediary_gender_func', 10, 5 );
function automate_intermediary_gender_func( $rel_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {
if( $rel_slug == 'swimming-event-playe' ) {
$post_id = 2310;
$value_to_copy = get_post_meta( $post_id, 'swimming-performance', true);
update_post_meta($post_id, 'swimming-performance-seconds', $value_to_copy);
}
}
NB: I do automatically get the $post_id
Yes, I created a many to many relationships for players and swimming and add some intermediary post type see it here hidden link
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Ademola,
Christian is currently out today, however he will be back tomorrow to continue working on this issue with you.
Thank you for the continued patience.
So the slug is swimming-event-player, not swimming-event-playe, right? Check the spelling of the slug please, because it is spelled differently in the URL you just provided.
Hello, apologies for the delay in response as I have been away for some days.
I created a many to many relationship to connect swimming and players with slug "swimming-event-player" and I also created an intermediary post type on that many to many relationship with slug "swimming-event-playe" see all of that here hidden link
Okay so as I was saying before, there is a typo in your code. The conditional for $rel_slug is wrong here:
if( $rel_slug == 'swimming-event-playe' ) {
The relationship slug is swimming-event-player, not swimming-event-playe as the code suggests. Also please note that you already have a function called automate_intermediary_gender_func in your custom code snippets. You'll need to rename this function to something other than automate_intermediary_gender_func to prevent a fatal error.