update_metadata() appends shortcode data for every page refresh

New to WordPress.

I have an implementation where I am writing multiple shortcodes on the admin panel and for every shortcode, I will update a post metadata key with every shortcode I find on the post.

This is the code to update the metadata inside my shortcode :

if (metadata_exists(‘post’, $postID, ‘shortcode_keys’)) {
$shortcode_keys[0] = get_post_meta($postID, ‘shortcode_keys’);
} else {
$shortcode_keys = array();
}
$new_key = ‘shortcode_widget_ids_’.sizeof($shortcode_keys);
array_push($shortcode_keys, $new_key);
update_metadata( ‘post’, $postID, ‘shortcode_keys’, $shortcode_keys, ” );

Expected :
1. Add 3 shortcodes on post.
2. Add information about 3 shortcodes using update_metadata(). So the metakey has array of length 3.

Actual :
1. Upon first refresh, the metakey shows array of length 3 as expected.
2. Hit refresh again, and the metakey holds a long complex array.

My guess is the metadata holds information even after refresh. I need metadata because I want to use this value elsewhere on the code. Is there some other way in wordpress to hold this array information with gets only data of the ‘n’ shortcodes I add to the post.

Read more here:: update_metadata() appends shortcode data for every page refresh

Leave a Reply

Your email address will not be published. Required fields are marked *