Skip Navigation

[Resolved] Unexpected results using in_array() in foreach loop for related child posts

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 Sean 10 months, 1 week ago.

Assisted by: Minesh.

Author
Posts
#2617795

Tell us what you are trying to do? Run a custom code snippet that loops through related child posts of a parent post

Is there any documentation that you are following? No

Is there a similar example that we can see? No

What is the link to your site?

I am currently running the following custom code snippet for a CRED form that edits a custom post type:

$salons = array( $salon1, $salon2, $salon3 );

if ( in_array( 'complete', $salons ) ) {

wp_set_post_terms( $post_id, 'local', 'tax-salon-location' );

} else {

wp_set_post_terms( $post_id, 'national', 'tax-salon-location' );

The above code snippet works just fine until I add it to another CRED form function that loops through related posts. The following code doesn't update the post taxonomy as expected. Cleary, I'm doing something wrong. Here is the other code snippet:

if ( $form_data['id']==1444 ) {

$related_owners = toolset_get_related_posts( $post_id, 'salon-related-owner', 'parent' );
$salons = array();

foreach ( $related_owners as $related_owner ) {

$salon_name1 = get_post_meta( $related_owner, 'wpcf-post-salon-name1', true );
$salon_name2 = get_post_meta( $related_owner, 'wpcf-post-salon-name2', true );
$salon_name3 = get_post_meta( $related_owner, 'wpcf-post-salon-name3', true );
$salon_details1 = get_post_meta( $related_owner, 'wpcf-post-salon-details1', true );
$salon_details2 = get_post_meta( $related_owner, 'wpcf-post-salon-details2', true );
$salon_details3 = get_post_meta( $related_owner, 'wpcf-post-salon-details3', true );

if ( !empty($salon_name1) && !empty($salon_details1) ) {

$salon1 = 'complete';
}

if ( !empty($salon_name2) && !empty($salon_details2) ) {

$salon2 = 'complete';
}

if ( !empty($salon_name3) && !empty($salon_details3) ) {

$salon3 = 'complete';
}

$salons = array( $salon1, $salon2, $salon3 );

if ( in_array( 'complete', $salons ) {

wp_set_post_terms( $related_owner, 'local', 'tax-salon-location' );

} else {

wp_set_post_terms( $related_owner, 'national', 'tax-salon-location' );
}
}
}

Any assistance would be greatly appreciated. Thank you in advance!

#2617813

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

This is a pure custom code that actually you will have to debut by adding some debug points and check with every line of code what is the value of your variables and then compare that if those variables having correct values or not.

If you still need further assistance I will require admin access details and problem URL and steps I will have to follow that should run the custom code you shared.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2617961

Thanks, Minesh! Let me try to debug this on my own first; hopefully I'll learn something that helps me in the future. I'll reach out if I still need assistance.

Thanks again!

#2618257

Minesh
Supporter

Languages: English (English )

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

Ok fine - Happy debugging 🙂

#2619201

Thank again, Minesh but I was able to solve this on my own. My issue is resolved now. Have a great a day!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.