Issue
I've got an array of data with which I'd like to update my products categories (taxanomy) metadata. Specifically, I'm trying to update description
as well as thumbnail url
values. I tried to use multiple wordpress functions but none of them worked! I didn't get any error but those values didn't get updated either.
$row_data = array(
'Term ID' => 150,
'Name' => "my 1st category",
'Slug' => "my-1st-category",
'Term URI' => "",
'Parent Term ID' => "",
'Description' => "My best description on this category that would change your life forever!",
'Display Type' => "",
'Image' => "https://myexample.site/wp-content/"
);
// This did not work!
wp_update_term($row_data['Term ID'], 'product_cat', $row_data);
// This did not work either!
update_term_meta($row_data['Term ID'], 'description', $row_data['Description']);
// This did not work either!
update_woocommerce_term_meta($row_data['Term ID'], 'thumbnail_id', $row_data['Image']);
Is there something that i'm missing?
Is thumbnail_id
the right field name that i'm using here?
Is update_woocommerce_term_meta
the right function for updating the thumbnail url?
Thank you.
Solution
The main problem come from your array keys that are wrong to insert or update a term.
From your custom array (updated):
$row_data = array(
'Term ID' => 150,
'Name' => 'my 1st category',
'Slug' => 'my-1st-category',
'Taxonomy' => 'product_cat'
'Parent Term ID' => 0,
'Description' => __("My best description on this category that would change your life forever!", "woocommerce"),
'Display Type' => "",
'Image ID' => "356",
);
- To update term description, use WordPress
wp_update_term()
function as follows:
wp_update_term( $row_data['Term ID'], $row_data['Taxonomy'], array('description' => $row_data['Description']) );
- To update the thumbnail ID (and not an Image URL), use
update_term_meta()
function as follows:
update_term_meta( $row_data['Term ID'], 'thumbnail_id', $row_data['Image ID'] );
Both are tested an works.
To insert a new term you will use WordPress wp_insert_term()
function like:
$term_data = wp_insert_term( $row_data['Name'], $row_data['Taxonomy'], array(
'description' => $row_data['Description'],
'slug' => $row_data['Slug'],
'parent' => $row_data['Parent Term ID'],
) );
if ( ! empty($row_data['Display Type']) ) {
update_term_meta( $row_data['Term ID'], 'display_type', $row_data['Display Type'] );
if ( ! empty($row_data['Image ID']) ) {
update_term_meta( $row_data['Term ID'], 'thumbnail_id', $row_data['Image ID'] );
}
Answered By - LoicTheAztec
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.