Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to unset/remove then replace/update a single value from a post meta field, but my code for array_push() and unset() are removing all of the values from each array.

Here are the two halves of the code I am currently using.

First, to find and remove the old value:

 $ID = $_GET["post_id"];
 $old = $entry["85"];
 $old_meta = array();
 $old_meta = get_post_meta($old,'_website',false);

 if(in_array($ID, $old_meta[current][items])){ 
       unset($old_meta[current][items][$ID]); 
  }

 update_post_meta($old,'_website',$old_meta);

Second to append the new value to the appropriate location:

$port = $entry["24"];
$new_meta = array();
$new_meta = get_post_meta($port,'_website',false);
$new_meta[content][items] = array();
array_push($new_meta[content][items],$ID);
update_post_meta($port,'_website',$new_meta);

It works to unset and insert the correct value, but any other values that were there (for both updating or unsetting) in the meta[current][items] array are removed

share|improve this question
    
I'm not entirely sure what are you trying to do, but you do some strange stuff in your code. What is $old = $entry['85'] (is it some post id)? Why are you setting $old_meta = array() if you assign new value right after that? And what is current and items (did you define such constants)? Also... What is structure of your meta currently? –  Krzysiek Dróżdż Jul 4 '13 at 20:48
    
if(in_array($ID, $old_meta[current][items])){ - is $ID a key or a value inside $old_meta? Because in_array checks to see if it is a value - but in the next line of code you use $ID as a key. –  vancoder Jul 4 '13 at 20:48
    
Try adding a print_r($new_meta) under $new_meta = get_post_meta($port,'_website',false); and post the results. –  vancoder Jul 4 '13 at 20:55
    
This is for a form plugin, so it is editing the array using $entry objects, the 'current' and 'items' constants are defined by their theme. The array is holding post IDs, and the $ID var is the current user's post ID, which is to be removed from the old array, and added to the new. –  Kyle Jul 4 '13 at 20:58
1  
I'll take it that by "array is holding post IDs" you mean the IDs are held as values, and not keys. That being the case unset($old_meta[current][items][$ID]); will not work, as there is no [$ID] key to unset. –  vancoder Jul 4 '13 at 21:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.