0

How can i update/remove/empty a sub-array on user post submission in admin area?

here ist the reference on how to update nested array i am using but it is not working

if ( $post->post_type == 'ds_product' ) {
     
    
    if ( isset( $_POST['meta'] ) ) { 
        if ( !empty($_POST['meta']['ds_product_gallery']) ){
             
            $gallery_data = []; 

            for ($i = 0; $i < count( $_POST['meta']['ds_product_gallery']['picture'] ); $i++ )
            {
                if ( !empty($_POST['meta']['ds_product_gallery']['picture'][ $i ]))
                {
                    $gallery_data['picture'][ ]    = $_POST['meta']['ds_product_gallery']['picture'][ $i ];
                    $gallery_data['figcaption'][ ] = $_POST['meta']['ds_product_gallery']['figcaption'][ $i ];
                }
            }

            if ( !empty($gallery_data) ) {
                $_POST['meta']['ds_product_gallery'] = $gallery_data;
            } 
        
        }else{  
            // this section doesn't clear the array
            $data = get_post_meta( $post->ID, 'meta', true );  
            $data['ds_product_gallery'][0][0] = [];
            update_post_meta( $post_ID, 'meta', $data[0] ); 
            
        } 
        foreach( $_POST['meta'] as $key => $value ){
            update_post_meta( $post_id, $key, $value );
        }
    } 
}

1 Answer 1

0

You would do it the same way as any other PHP application, this isn't a WordPress problem.

To remove an item from an array in PHP, use unset( thing to remove ).

E.g.

$test = [ 'banana', 'cucumber', 'apple' ];
unset( $test[1] );
// cucumber is no longer in the test array, and the array now has 2 not 3 items
9
  • have a look at the else section that is exactly what i am doing but it doesn't work i even tried $data = get_post_meta( $post->ID, 'meta', true ); $data['ds_product_gallery'] = []; update_post_meta( $post_ID, 'meta', $data ); without success Mar 8, 2022 at 14:48
  • $data['ds_product_gallery'][0][0] = []; sets it to an empty array, if you want to remove the item from the array though then that's a general PHP problem not a WordPress problem, you can rephrase this question as "How do I remove an item from an array", use unset().
    – Tom J Nowell
    Mar 8, 2022 at 14:51
  • ok mate, but how do you update the meta then? i tried $data = get_post_meta( $post->ID, 'meta', true ); unset($data['ds_product_gallery']); update_post_meta( $post_ID, 'meta', $data ); but nothing Mar 8, 2022 at 14:53
  • you update the meta by grabbing its value, changing that value, then updating it, you've already figured out that part
    – Tom J Nowell
    Mar 8, 2022 at 14:54
  • Here's the relevant doc: php.net/manual/en/function.unset.php, as I said, this isn't a WordPress problem, it's a generic PHP problem. If your meta still has the values you tried to remove, it's because you did not remove them successfully. Either that or the code you're trying to debug is never run to begin with
    – Tom J Nowell
    Mar 8, 2022 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.