Skip Navigation

[Resolved] Display Data in a table from multiple post types

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

This topic contains 40 replies, has 3 voices.

Last updated by Christian Cox 4 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1503109

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 );

#1503609

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.

#1504545

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

#1505247

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.

#1510575

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

#1511561

Is this slug correct?

if( $rel_slug == 'swimming-event-playe' ) {
#1512289

Yes, I created a many to many relationships for players and swimming and add some intermediary post type see it here hidden link

#1513467

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.

#1515351

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.

#1531523

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

#1535965

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.