user29351553
user29351553

Reputation: 1

The parent attribute of the wp_insert_term function only works on one level

Would anyone have the solution to this problem that has been poisoning my life for 2 days already?

Here is my extremely simplified code:

<?php 
// Creation of the first level: "niveau 0"
$insert_structure = wp_insert_term(
    "niveau 0", 
    'product_cat', 
    array(
        "slug" => sanitize_title_with_dashes("niveau 0"), 
    )
);

if ( ! is_wp_error( $insert_structure ) ) {
    $id_structure = isset( $insert_structure['term_id'] ) ? intval($insert_structure['term_id']) : 0;
} else {
     echo $insert_structure->get_error_message();
}

// Creation of the second level: "niveau 1"
$insert_n1 = wp_insert_term(
    "niveau 1", 
    'product_cat', 
    array(
        "slug" => sanitize_title_with_dashes("niveau 1"), 
        "parent" => $id_structure, 
    )
);

if ( ! is_wp_error( $insert_n1 ) ) {
    $id_n1 = isset( $insert_n1['term_id'] ) ? intval($insert_n1['term_id']) : 0;
} else {
     echo $insert_n1->get_error_message();
}

// Creation of the third level: "niveau 2"
$insert_n2 = wp_insert_term(
    "niveau 2", 
    'product_cat', 
    array(
        "slug" => sanitize_title_with_dashes("niveau 2"), 
        "parent" => $id_s1, 
    )
);

if ( ! is_wp_error( $insert_n2 ) ) {
    $id_n2 = isset( $insert_n2['term_id'] ) ? intval($insert_n2['term_id']) : 0;
} else {
     echo $insert_n2->get_error_message();
}
?>

Here's what it gives in wordpress: enter image description here

How is it that the second level (niveau 1) is well linked to the first (niveau 0) but not the third (niveau 2)?

It is exactly the same command used three times with the "parent" argument of entered for the two subcategories.

Thank you in advance for your help!

Upvotes: 0

Views: 34

Answers (1)

JakeParis
JakeParis

Reputation: 11210

Because you have "parent" => $id_s1 instead of "parent" => $id_n1. The typo is s1 rather than n1

Upvotes: 0

Related Questions