I'm extending the Woocommerce Product CSV Import functionality to import more data.
I'm inserting a CPT with 2 metadata, a string (URL) and a bool, and the post_meta inserted gets messed up. It inserts an array for the meta which should have the bool.
Snaptshot of code:
$link = array(
'url' => $url // string
'caption' => $caption // string
'hide' => $hide // bol
$meta_input = array(
'ct_external_link_url' => $link['url']
);
if( isset($link['hide']) ) {
$meta_input += ['ct_hide_from_frontent' => $link['hide']];
}
$postarr = array(
'post_title' => $link['caption'],
'post_type' => 'ct_external_link',
'post_status' => 'publish',
'post_excerpt' => $link['caption'],
'meta_input' => $meta_input
);
write_log("Args to insert external link post:"); // write_log ouputs to debug.txt
write_log($postarr);
$external_link_id = wp_insert_post($postarr);
$external_link = get_post($external_link_id, 'ARRAY_A');
if( !is_null($external_link)) {
write_log("External link post:");
write_log($external_link);
write_log("External link post meta:");
$meta = get_post_meta($external_link_id);
write_log($meta);
}
Since I'm writing logs, these come out as following:
[15-Mar-2019 10:33:45 UTC] Args to insert external link post:
[15-Mar-2019 10:33:45 UTC] Array
(
[post_title] => VÃdeo 1
[post_type] => ct_external_link
[post_status] => publish
[post_excerpt] => VÃdeo 1
[meta_input] => Array
(
[ct_external_link_url] => ;ab_channel=CollegeMusic
[ct_hide_from_frontent] => 1
)
)
[15-Mar-2019 10:33:45 UTC] External link post meta:
[15-Mar-2019 10:33:45 UTC] Array
(
[ct_hide_from_frontent] => Array
(
[0] => 1
)
)
Note that, while the value for key 'ct_hide_from_frontent' is a bool in the meta_input, it comes out from the post_meta as an array. Why?
Thanks!
I'm extending the Woocommerce Product CSV Import functionality to import more data.
I'm inserting a CPT with 2 metadata, a string (URL) and a bool, and the post_meta inserted gets messed up. It inserts an array for the meta which should have the bool.
Snaptshot of code:
$link = array(
'url' => $url // string
'caption' => $caption // string
'hide' => $hide // bol
$meta_input = array(
'ct_external_link_url' => $link['url']
);
if( isset($link['hide']) ) {
$meta_input += ['ct_hide_from_frontent' => $link['hide']];
}
$postarr = array(
'post_title' => $link['caption'],
'post_type' => 'ct_external_link',
'post_status' => 'publish',
'post_excerpt' => $link['caption'],
'meta_input' => $meta_input
);
write_log("Args to insert external link post:"); // write_log ouputs to debug.txt
write_log($postarr);
$external_link_id = wp_insert_post($postarr);
$external_link = get_post($external_link_id, 'ARRAY_A');
if( !is_null($external_link)) {
write_log("External link post:");
write_log($external_link);
write_log("External link post meta:");
$meta = get_post_meta($external_link_id);
write_log($meta);
}
Since I'm writing logs, these come out as following:
[15-Mar-2019 10:33:45 UTC] Args to insert external link post:
[15-Mar-2019 10:33:45 UTC] Array
(
[post_title] => VÃdeo 1
[post_type] => ct_external_link
[post_status] => publish
[post_excerpt] => VÃdeo 1
[meta_input] => Array
(
[ct_external_link_url] => https://www.youtube.com/watch?v=F0IbjVq-fgs&ab_channel=CollegeMusic
[ct_hide_from_frontent] => 1
)
)
[15-Mar-2019 10:33:45 UTC] External link post meta:
[15-Mar-2019 10:33:45 UTC] Array
(
[ct_hide_from_frontent] => Array
(
[0] => 1
)
)
Note that, while the value for key 'ct_hide_from_frontent' is a bool in the meta_input, it comes out from the post_meta as an array. Why?
Thanks!
So I ended up just adding the meta after inserting the post, which doesn't give any error. It bypasses the problem without explaining it though...
$postarr = array(
'post_title' => $link['caption'],
'post_type' => 'ct_external_link',
'post_status' => 'publish',
'post_excerpt' => $link['caption'],
);
$external_link_id = wp_insert_post($postarr);
if( !empty($external_link_id) ) {
add_post_meta($external_link_id, 'ct_external_link_url', $doc['file'], true);
if( isset($doc['hide']) ) {
add_post_meta($external_link_id, 'ct_hide_from_frontent', $doc['hide']);
}
}