skip to Main Content

everyone

I would like to delete a value from the postmeta in my wordpress installation – and not a complete metavalue, but only part of the content in it.

e.g. metakey = test
metavalue = abba; dancing; queen;

and the "dancing" should be thrown out.
With update_post_meta and delete_post_meta I only manage to delete the complete metavalue or metakey.
update_post_meta( $id, ‘test’, $dancing);
delete_post_meta( $id, ‘test’, $dancing);
dont work 🙁

how can i remove only part of it?

lg
yeah

2

Answers


  1. I think it works.
    First I get the value of the previous postmeta.
    Then we change it and finally save it in our post.
    The order is as follows:

    $test_meta_key = get_post_meta($post_id, 'meta_key');
    
    $test_meta_key = str_replace('dancing', '', $test_meta_key);
    
    update_post_meta($post_id, 'meta_key', $test_meta_key);
    
    Login or Signup to reply.
  2. just remove the dancing word from the desired metavalue. You don’t have to serialize if meta value is an array, wordpress will automatically do it for you.

    $post_id    = 1;
    $metakey    = 'test'; 
    $metavalue  = "abba; dancing; queen";
    $metavalue  = "abba; queen"; // updated meta value
    update_post_meta( $post_id, $metakey, $metavalue );
    

    if $metavalue is array

    $metavalue = [ "abba", "dancing", "queen" ];
    $metavalue = [ "abba", "queen" ]; // updated meta value
    update_post_meta( $post_id, $metakey, $metavalue );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search