Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Add/Remove Single Value From Post Meta Array WordPress

  • SOLVED

I'm trying to find and remove a single value from a multidimensional array stored in a post meta field and likewise add a single value to the same field, but I am having some problems.

Here is what I've been trying:

$ID = $_GET["gform_post_id"];

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

if(in_array($ID, $old_meta['content']['items'])){
foreach($old_meta['content']['items'] as $key => $val) {
if($val == $ID) {
unset($old_meta['content']['items'][$key]);
}
}
}

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

$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);


This simply isn't working, and I'm stuck. I haven't worked much with arrays like this so I'm struggling.

Here is an example of the field:

array(1) { [0]=> array(3) { ["content"]=> array(1) { ["items"]=> array(2) { [0]=> string(3) "716" [1]=> string(3) "710" } } ["options"]=> array(8) { ["title"]=> string(7) "inherit" ["size"]=> string(4) "tiny" ["orderby"]=> string(4) "date" ["order"]=> string(4) "desc" ["filter"]=> string(8) "category" ["pagination"]=> bool(false) ["per_page"]=> int(12) ["content"]=> array(3) { [0]=> string(5) "title" [1]=> string(7) "excerpt" [2]=> string(4) "tags" } } ["__version"]=> string(5) "2.3.1" } }

So the add/remove would be trying to take out "716" there or adding another in, etc.

Answers (1)

2013-07-05

Giri answers:

I think you misspelled "content" as "current" in this code

if(in_array($ID, $old_meta['current']['items'])){

foreach($old_meta['current']['items'] as $key => $val) {

if($val == $ID) {

unset($old_meta['current']['items'][$key]);

}

}

}


Kyle comments:

Thanks for the reply

Yeah, sorry. That was just a last minute attempt from a stackexchange suggestion


Giri comments:

Is it working now?


Kyle comments:

Nope

I updated above to current code


Giri comments:

$old_meta = get_post_meta($old,'_website',false);

In that line $old value should be an integer. So change

$old = $entry["85"];

to

$old = 85;


Kyle comments:

The entry is an integer.


Giri comments:

Can you post the var_dump of $old_meta before and after unset?

I mean place this code

var_dump($old_meta);

after

$old_meta = get_post_meta($old,'_website',false);

and before

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





Kyle comments:

http://pastie.org/8112974


Giri comments:

Try this code

$ID = $_GET["gform_post_id"];



$old = $entry["85"];

$old_meta = array();

$old_meta = get_post_meta($old,'_website',false);



if(in_array($ID, $old_meta[0][0]['content']['items'])){

foreach($old_meta[0][0]['content']['items'] as $key => $val) {

if($val == $ID) {

unset($old_meta[0][0]['content']['items'][$key]);

}

}

}



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



$port = $entry["24"];

$new_meta = array();

$new_meta = get_post_meta($port,'_website',false);

$new_meta[0][0]['content']['items'] = array();



array_push($new_meta[0][0]['content']['items'],$ID);



update_post_meta($port,'_website',$new_meta);


Kyle comments:

The code I sent in the pastie is wrong, it does not accurately store the values, that's the mistake code. The code above in the question with the sample array is how it should look.


Giri comments:

In that case try this one

$ID = $_GET["gform_post_id"];



$old = $entry["85"];

$old_meta = array();

$old_meta = get_post_meta($old,'_website',false);



if(in_array($ID, $old_meta[0]['content']['items'])){

foreach($old_meta[0]['content']['items'] as $key => $val) {

if($val == $ID) {

unset($old_meta[0]['content']['items'][$key]);

}

}

}



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



$port = $entry["24"];

$new_meta = array();

$new_meta = get_post_meta($port,'_website',false);

$new_meta[0]['content']['items'] = array();



array_push($new_meta[0]['content']['items'],$ID);



update_post_meta($port,'_website',$new_meta);


Kyle comments:

No luck, and now its not add the new value at all


Giri comments:

Hey, Its really hard for me to understand what wrong without seeing the actual dump. Can you give me your ftp details? I'll check whats wrong and fix it in few minutes.