Skip Navigation

[Attente de la confirmation de l'utilisateur] Problem with code

This support ticket is created Il y a 3 jours et 10 heures. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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)

Ce sujet contient 1 réponse, a 1 voix.

Dernière mise à jour par Minesh Il y a 2 jours et 20 heures.

Assisté par: Minesh.

Auteur
Publications
#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

Les langues: Anglais (English )

Fuseau horaire: 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');