Skip Navigation

[Waiting for user confirmation] Problem with code

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 1 reply, has 1 voice.

Last updated by Minesh 2 days, 22 hours ago.

Assisted by: Minesh.

Author
Posts
#2786153

I am trying to:

To use this fonction and that doesn't work.

My slug is formation and relation ship is categorie.

I would like to display the id of post relationship

Thanks alot

//TEST
function custom_post_id_by_slug_and_related_shortcode() {
// Récupérer l'ID du post formation comme auparavant
$post_id = 18248; // Assure-toi que cette fonction renvoie l'ID correct du post formation

// Utiliser la clé spécifique pour trouver l'ID du post parent 'categorie'
$related_post_id = get_post_meta($post_id, '_wpcf_belongs_categorie_id', true);

// Construire la réponse
if (!empty($related_post_id)) {
return "ID de 'formation': " . esc_html($post_id) . " | ID de 'categorie': " . esc_html($related_post_id);
} else {
return "ID de 'formation': " . esc_html($post_id) . " | Aucun post lié trouvé pour cette catégorie";
}
}
add_shortcode('show_post_id_by_slug_and_related', 'custom_post_id_by_slug_and_related_shortcode');

#2786196

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

You are using old post-relationship method to retrieve the parent post ID and that is not correct.

With new post-relationship, you should try to use post-relationship API function: toolset_get_related_post() in order to get the related post ID.
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

Can you please try to use the following modified code and check if that help you to resolve your issue:

function custom_post_id_by_slug_and_related_shortcode() {
// Récupérer l'ID du post formation comme auparavant
$post_id = 18248; // Assure-toi que cette fonction renvoie l'ID correct du post formation

// Utiliser la clé spécifique pour trouver l'ID du post parent 'categorie'
$related_post_id = toolset_get_related_post($post_id,'categorie','parent');

// Construire la réponse
if (!empty($related_post_id)) {
return "ID de 'formation': " . esc_html($post_id) . " | ID de 'categorie': " . esc_html($related_post_id);
} else {
return "ID de 'formation': " . esc_html($post_id) . " | Aucun post lié trouvé pour cette catégorie";
}
}
add_shortcode('show_post_id_by_slug_and_related', 'custom_post_id_by_slug_and_related_shortcode');