Update Post Meta not working for Custom Post Type
Question
I am trying to fetch the data from the database and update the post meta. Everything is perfect. I have even checked each variable. The method is also firing only once. The problem is that in the database I am still seeing the array but with blank values. a:6:{i:57;s:0:"";i:58;s:0:"";i:59;s:0:"";i:60;s:0:"";i:61;s:0:"";i:62;s:0:"";}
I really need a solution to why this update_post_meta is not working. I have even tried add_post_meta instead and it doesn’t work too.
function slug_save_post_callback($post_ID, $post, $update)
{
if ($post->post_type != 'aps-products' || $post->post_status == 'auto-draft')
return;
if ($post->post_date_gmt != $post->post_modified_gmt)
return;
$result = getSpecs($post->post_title);
$new_slug = sanitize_title($result[0] . ' ' . $result[1], $post_ID);
if ($new_slug == $post->post_name)
return;
// unhook this function to prevent infinite looping
remove_action('save_post_aps-products', 'slug_save_post_callback', 100, 3);
// update the post slug (WP handles unique post slug)
wp_update_post(array(
'ID' => $post_ID,
'post_name' => $new_slug,
'post_title' => $result[0] . ' ' . $result[1]
));
// get aps attributes data from input fields
$groups = get_aps_cat_groups(74);
$groups_data = get_aps_groups_data();
$attrs_data = get_aps_attributes_data();
error_log("Executing");
foreach ($groups as $groupId) {
$group_data = $groups_data[$groupId];
$group_values = get_aps_product_attributes($post->ID, $groupId);
if ($group_data['attrs']) {
if (aps_is_array($group_data['attrs'])) {
// $frist = array();
$value = '';
foreach ($group_data['attrs'] as $attr_id) {
switch ($attr_id) {
case 2:
$value = "Smartphone";
break;
case 3:
$value = $result[1];
break;
}
$frist[$attr_id] = $value;
}
update_post_meta($post_ID, 'aps-attr-group-' . $groupId, $frist);
$frist=[];
}
}
}
add_action('save_post_aps-products', 'slug_save_post_callback', 100, 3);
}
add_action('save_post_aps-products', 'slug_save_post_callback', 100, 3);
0
actions, custom-post-types, hooks, plugin-development, post-meta
3 years
2020-04-06T08:51:02-05:00
2020-04-06T08:51:02-05:00 0 Answers
99 views
0
Leave an answer