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

deleting element from a serialized array upon deletion of a post? WordPress

  • SOLVED

I want to delete an element (post id) from a serialized array associated with a specific meta_key. The following screenshot tells the whole story.

What you're seeing in attached screenshots are, post ids saved in a serialized array. Upon deletion of any post the id of the post should be removed from meta_value where (everywhere in user_meta) meta_key is "whatever". I want to wrap unset for elements (both key and value) inside a function as shown below.

I need something like this..

add_action('before_delete_post', 'upon_deleting_post');
function upon_deleting_post($post_id){
delete_metadata ();
};

Answers (3)

2014-01-25

Hariprasad Vijayan answers:

Hello,

Try this

add_action('before_delete_post', 'upon_deleting_post');

function upon_deleting_post($post_id){
$whatever= get_post_meta( $post_id, 'whatever' );
foreach( $whatever as $value ) {
delete_post_meta( $post_id, 'whatever', $value );
}

};


waqas comments:

No, I didn't work. I think you're missing many things in delete_post_meta_by_key('whatever');


Hariprasad Vijayan comments:

I had updated the code. Try latest one please.


Hariprasad Vijayan comments:

Sorry. I misunderstand the question. Above code won't work.


Hariprasad Vijayan comments:

How many users you have? only single user?


Hariprasad Vijayan comments:

Try this

add_action('before_delete_post', 'upon_deleting_post');
function upon_deleting_post($post_id){
$whatever= get_user_meta( 1, 'whatever' ,true);
$new_array = array();
foreach( $whatever as $value ) {
if($value != $post_id)
{
$new_array[] = $value ;
}
}
update_user_meta( 1, 'whatever', $new_array);
}


waqas comments:

There are so many users.


Hariprasad Vijayan comments:

is that code working?


Hariprasad Vijayan comments:

In the case of multiple user the code would be like this

add_action('before_delete_post', 'upon_deleting_post');
function upon_deleting_post($post_id){

$post_details = get_post($post_id, ARRAY_A);
$post_author = $post_details['post_author'];
$whatever= get_user_meta( $post_author, 'whatever' ,true);
$new_array = array();
foreach( $whatever as $value ) {
if($value != $post_id)
{
$new_array[] = $value;
}
}
update_user_meta( $post_author, 'whatever', $new_array);
}

Let me know if you have any trouble.


waqas comments:

Nope, I got this

<blockquote>Warning: Invalid argument supplied for foreach() in /../wp-content/themes/twentyfourteen/functions.php on line 934

Warning: Cannot modify header information - headers already sent by (output started at /.../wp-content/themes/twentyfourteen/functions.php:934) in /h.../[removed at the request of the site owner]/wp-includes/pluggable.php on line 896</blockquote>


Hariprasad Vijayan comments:

meta for "whatever" is empty?
Also check there is any blank lines in your function.php


Hariprasad Vijayan comments:

<blockquote>Warning: Invalid argument supplied for foreach() in /../wp-content/themes/twentyfourteen/functions.php on line 934</blockquote>
-Reason - the meta_value is empty for meta_key "whatever". Please check table.
<blockquote>Warning: Cannot modify header information - headers already sent by (output started at /.../wp-content/themes/twentyfourteen/functions.php:934) in /h.../[removed at the request of the site owner]/wp-includes/pluggable.php on line 896</blockquote>
This is because there is some blank line in your function.php. probable bottom of your function.php


Hariprasad Vijayan comments:

<blockquote>Warning: Invalid argument supplied for foreach() in /../wp-content/themes/twentyfourteen/functions.php on line 934</blockquote>
-Reason - the meta_value is empty for meta_key "whatever". Please check table.
<blockquote>Warning: Cannot modify header information - headers already sent by (output started at /.../wp-content/themes/twentyfourteen/functions.php:934) in /h.../[removed at the request of the site owner]/wp-includes/pluggable.php on line 896</blockquote>
This is because there is some blank line in your function.php. probably bottom of your function.php


waqas comments:

Yes, I checked. there are no blank spaces. It shows error upon deleting post permanently. And changes the meta_value from s:34:"a:2:{i:0;s:3:"498";i:1;s:3:"496";}"; to a:0:{}. Removes everything.


Hariprasad Vijayan comments:


add_action( 'delete_post', 'upon_deleting_post', 10 );
function upon_deleting_post($post_id){

$whatever= get_user_meta( 1, 'whatever' ,true);
$new_array = array();
foreach( $whatever as $value ) {
if($value != $post_id)
{
$new_array[] = $value;
}
}
update_user_meta( 1, 'whatever', $new_array);
}


waqas comments:

Same error again. I just tried to understand your code. And its funny to know that you're checking if($value != $post_id) whereas post_id should be check for availability

And a $new_array[] = $value; thats why it deletes the whole array (s:34:"a:2:{i:0;s:3:"498";i:1;s:3:"496";}";) and replaced by empty a:0:{}

Shouldn't we use


foreach( $whatever as $value ) {
if($value == $post_id)
{
unset($array[$value]);
}
}


Pardon me if I'm wrong. Im not php guy :D


Hariprasad Vijayan comments:

Yes. It can also do like this

add_action( 'delete_post', 'upon_deleting_post', 10 );
function upon_deleting_post($post_id){
$whatever= get_user_meta( 1, 'whatever' ,true);
if (($key = array_search($post_id, $whatever)) !== false) {
unset($whatever[$key]);
}
update_user_meta( 1, 'whatever', $whatever);
}


waqas comments:

<blockquote>Warning: array_search() [function.array-search]: Wrong datatype for second argument in /home1/.../wp-content/themes/twentyfourteen/functions.php on line 932

Warning: Cannot modify header information - headers already sent by (output started at /home1/.../wp-content/themes/twentyfourteen/functions.php:932) in /home1/.../wp-includes/pluggable.php on line 896</blockquote>

I tried the above code and the follow code as well, Upon deletion of post both generated the same error. Post is deleted but meta_values are unchanged. It is possible. We're close but not getting it right.

+ in above code you're considering single user only.

add_action( 'delete_post', 'upon_deleting_post', 10 );
function upon_deleting_post ($post_id){
$whatever= get_user_meta( 1, 'whatever' ,true);
$key = array_search($post_id, $whatever);
if($key!==false){
unset($array[$key]);
}
update_user_meta( 1, 'whatever', $whatever);
}


Hariprasad Vijayan comments:

Try this

add_action( 'delete_post', 'upon_deleting_post', 10 );

function upon_deleting_post ($post_id)
{
$users = get_users();
foreach ($users as $user) {
$whatever = get_user_meta( $user->ID, 'whatever' ,true);
if(!empty($whatever))
{
$key = array_search($post_id, $whatever);
if($key!==false){
unset($whatever[$key]);
}
update_user_meta( $user->ID, 'whatever', $whatever);
}
}
}


waqas comments:

<blockquote>Warning: array_search() [function.array-search]: Wrong datatype for second argument in</blockquote>

I think what you're ignoring is serialization

Thanks anyway. Arnav Joy has the right solution.

2014-01-25

Francisco Javier Carazo Gil answers:

Hello,

You need something like this:

function delete_metadata ($post_id, $meta_key){
$meta_values = get_post_meta( $post_id, $meta_key);
$position = array_search($post_id, $meta_values);
unset($meta_value[$position]);
$meta_values = array_value($meta_values); // to normalize the array
update_post_meta($post_id, $meta_key, $meta_values);

}


Francisco Javier Carazo Gil comments:

Supposing post_id is saved inside an array with more data in postmeta table with a meta_key defined by $meta_key.


waqas comments:

Shouldn't I universalize $position before deleting?


Francisco Javier Carazo Gil comments:

No, position is automatically found by array_search, don't worry about this.


waqas comments:

I'm trying to understand the function. It looks the code you've mentioned will dell something from post_meta..

My screenshot is about user_meta :D


Francisco Javier Carazo Gil comments:

Ah ok, sorry :)

Only some changes:.

function delete_metadata ($user_id, $post_id, $meta_key){
$meta_values = get_user_meta( $user_id, $meta_key);
$position = array_search($post_id, $meta_values);
unset($meta_value[$position]);
$meta_values = array_value($meta_values); // to normalize the array
update_user_meta($user_id, $meta_key, $meta_values);
}


waqas comments:

Ok friend, I just tried it like this:

define('DEL_META_KEY', "whatever");
define("DEL_META_KEY", "bookmarks");
function delete_metadata ($user_id, $post_id, DEL_META_KEY) {
$meta_values = get_user_meta ($user_id, DEL_META_KEY);
$position = array_search ($post_id, $meta_values);
unset ($meta_value[$position]);
$meta_values = array_value ($meta_values); // to normalize the array
update_user_meta ($user_id, DEL_META_KEY, $meta_values);
}


and I'm getting <blockquote>Parse error: syntax error, unexpected ')', expecting '&' or T_VARIABLE in /home1/..../twentyfourteen/functions.php on line 931</blockquote>

Line 931 is function delete_metadata ($user_id, $post_id, DEL_META_KEY) {


Francisco Javier Carazo Gil comments:

Tell me which is this line and remove DEL_META_KEY from the parameters: function delete_metadata ($user_id, $post_id) {


waqas comments:

Ok now I deleted DEL_META_KEY from function delete_metadata($user_id, $post_id) {

Fatal error: Cannot redeclare delete_metadata() (previously declared in /.../wp-includes/meta.php:186) in /.../wp-content/themes/twentyfourteen/functions.php on line 936

line 936 is end of function having "}"

2014-01-25

Arnav Joy answers:

<?php

add_action( 'delete_post', 'delete_post_from_array', 10 );

function delete_post_from_array( $pid ) {
$whatever = get_user_meta(1,'whatever',true);

$new_post_ids = array();

if( !empty( $whatever ) ){
foreach( $whatever as $post_id ){
if( $post_id == $pid )
continue;
else
$new_post_ids[] = $pid;
}
}

update_user_meta(1,'whatever',$new_post_ids);

}


waqas comments:

ohoo, It deleted everything from the array. Now its showing a:0:{}


Arnav Joy comments:

ohh , try this

add_action( 'delete_post', 'delete_post_from_array', 10 );

function delete_post_from_array( $pid ) {
$whatever = get_user_meta(1,'whatever',true);

$new_post_ids = array();

if( !empty( $whatever ) ){
foreach( $whatever as $post_id ){
if( $post_id == $pid )
continue;
else
$new_post_ids[] = $post_id;
}
}

update_user_meta(1,'whatever',$new_post_ids);

}


waqas comments:

What if there are many users having same post id saved in their user_meta?


Arnav Joy comments:

try this

<?php

add_action( 'delete_post', 'delete_post_from_array', 10 );

function delete_post_from_array( $pid ) {
$blogusers = get_users();

if( !empty( $blogusers ) ) {

foreach( $blogusers as $blogusr ){
$whatever = get_user_meta( $blogusr->ID ,'whatever',true);

$new_post_ids = array();

if( !empty( $whatever ) ){
foreach( $whatever as $post_id ){
if( $post_id == $pid )
continue;
else
$new_post_ids[] = $post_id;
}
}

update_user_meta( $blogusr->ID ,'whatever',$new_post_ids);

}

}


waqas comments:

<blockquote>Warning: Invalid argument supplied for foreach() in /.../wp-content/themes/twentyfourteen/functions.php on line 937

Warning: Cannot modify header information - headers already sent by (output started at /.../wp-content/themes/twentyfourteen/functions.php:937) in /.../wp-includes/pluggable.php on line 896</blockquote>

line 937 contains foreach( $whatever as $post_id ){


Arnav Joy comments:

can you show me output of for follwing

add print_r($whatever);
after line

$whatever = get_user_meta( $blogusr->ID ,'whatever',true);

so it will become

$whatever = get_user_meta( $blogusr->ID ,'whatever',true);

print_r($whatever);


Arnav Joy comments:

also make sure , key should be "whatever" otherwise replace it with yours meta key


waqas comments:

<strong>print_r($whatever); </strong>

1a:11:{i:0;s:3:"494";i:1;s:3:"481";i:2;s:3:"479";i:3;s:3:"466";i:4;s:3:"463";i:5;s:3:"461";i:6;s:3:"459";i:7;s:3:"446";i:8;s:3:"452";i:9;s:3:"454";i:10;s:3:"457";}


<strong>and print_r(unserialize($whatever));</strong>
Array
(
[0] => 494
[1] => 481
[2] => 479
[3] => 466
[4] => 463
[5] => 461
[6] => 459
[7] => 446
[8] => 452
[9] => 454
[10] => 457
)


Arnav Joy comments:

try this now

<?php

add_action( 'delete_post', 'delete_post_from_array', 10 );

function delete_post_from_array( $pid ) {
$blogusers = get_users();

if( !empty( $blogusers ) ) {

foreach( $blogusers as $blogusr ){
$whatever = get_user_meta( $blogusr->ID ,'whatever',true);
$whatever = unserialize($whatever) ;

$new_post_ids = array();

if( !empty( $whatever ) ){
foreach( $whatever as $post_id ){
if( $post_id == $pid )
continue;
else
$new_post_ids[] = $post_id;
}
}

update_user_meta( $blogusr->ID ,'whatever',serialize($new_post_ids));

}

}


waqas comments:

Btw your last code (01/25/14 7:32pm) makes sense but It appears problem is somewhere between following code.


foreach( $whatever as $post_id ){
if( $post_id == $pid )
continue;
else
$new_post_ids[] = $post_id;
}


Arnav Joy comments:

what problem you are getting now.


waqas comments:

Thanks Arnav Joy, Its working for all users. very clean and nice. Now you're my go to guy ;)