Reputation:
I have created a import tool to import data from a json file to create posts in my custom post type. It all works well and I can import acf fields as well as assigned terms. My question is, how can I import child term assigned to the term.
I have two variables right now that collects the data. I would like the have the variable eventCategoryChildName to assign its vaule as a child term to eventCategoryID
$eventCategoryChildName = $ev['OfficialCity'];
$eventCategoryId = $ev['RegionID'];
Here is how the import of terms functions right now without child terms:
// set category terms
$cat_ids = $eventCategoryName;
// Add these categories, note the last argument is true.
wp_set_object_terms( $post_id, $cat_ids, 'lan', true );
Edit:
So I managed to import the child associated to the right parents but they are not checked to the post
// set category terms
$cat_ids = $eventCategoryName;
// Import category name
$term_taxonomy_ids = wp_set_object_terms( $post_id, $cat_ids, 'lan', true );
$parent_term = wp_set_object_terms( $post_id, $cat_ids, 'lan', true );
// Check terms import for errors
if ( is_wp_error( $term_taxonomy_ids ) ) {
// There was an error somewhere and the terms couldn't be set.
echo $return->get_error_message();
} else {
// Success! These categories were added to the post.
}
$parent_term = term_exists( $eventCategoryName, 'lan' );
wp_insert_term(
$eventCategoryChildName, // Customize as you wish
'lan',
array(
'parent' => $parent_term['term_id'],
'slug' => $eventCategoryChildName // Customize as you wish
)
);
if ( !is_wp_error( $child_term_result ) ) {
wp_set_object_terms( $post_id, $child_term_result['term_id'], 'lan', true );
}
Upvotes: 1
Views: 653
Reputation: 598
Firstly, you should check the result of wp_set_object_terms
, to see if an error was returned:
$term_taxonomy_ids = wp_set_object_terms( $post_id, $cat_ids, 'lan', true );
if ( is_wp_error( $term_taxonomy_ids ) ) {
// There was an error somewhere and the terms couldn't be set.
echo $return->get_error_message();
} else {
// Success! These categories were added to the post.
}
Then, the term you want to add may not exists. In such case, wp_set_object_terms
will return an invalid_taxonomy
error. So you may want to add such missing term to your taxonomy:
wp_insert_term(
'Term Name', // Customize as you wish
'lan',
array(
'description' => 'Term desc', // Customize as you wish
'slug' => 'lan-slug1' // Customize as you wish
)
);
// For child term:
$parent_term = term_exists( 'parent_term_id', 'lan' );
$child_term_result = wp_insert_term(
'Child Term Name', // Customize as you wish
'lan',
array(
'parent' => $parent_term['term_id']
'description' => 'Term desc', // Customize as you wish
'slug' => 'lan-slug1' // Customize as you wish
)
);
// Add the child term to the post
if ( !is_wp_error( $child_term_result ) ) {
wp_set_object_terms( $post_id, $child_term_result['term_id'], 'lan', true );
}
Finally, be sure you execute this code after -at least- the init
hook of Wordpress. For instance:
function parse_my_json() {
// Your code here
}
add_action('init','parse_my_json');
Useful references:
Upvotes: 1