0

update_post_meta() keeps adding to an array instead of replacing value. Any tips?

Previously I used add_post_meta() by accident and perhaps that somehow flagged the field as an array?

Update

So how can I set a meta field to NOT be an array?

code:

$lowjobtrig = 1;    
foreach ( $values as $value ) :


    //====================myedit==================

    if ($key == 'public_paywall') {
        if ($value == 'paywall') {
            add_post_meta($postId, $key, $value, /*unique=*/ false);
        } elseif ($value == 'public'{
            add_post_meta($postId, $key, $value, /*unique=*/ false);                        
        } 

    } elseif ($key == 'pay_offer') {
        if ($value == 'pay') {
            add_post_meta($postId, $key, $value, false);
        } elseif ($value == 'no pay') {
            add_post_meta($postId, $key, $value, false);    
        } else {
        }

    } elseif ($key == 'low_jobs' AND $value == 'under 100'){
        $lowjobtrig = 1;
        update_post_meta($postId, $key, 'under 100');   

    } elseif ($key == 'low_jobs' AND $value != 'under 100' AND $lowjobtrig == 0){
        add_post_meta($postId, $key, 'OVER 100', true); 
        $lowjobtrig = 1;             
    }

    else {
        FeedWordPress::diagnostic('syndicated_posts:meta_data', ">>>>>>>>>>ELSE Adding post meta-datum to post [$postId]: [$key] = ".MyPHP::val($value, /*no newlines=*/ true));                            
        add_post_meta($postId, $key, $value, /*unique=*/ false);

    }
2
  • 1
    Update_post_meta should not do that. Could you show us your current code? Commented Jun 3, 2019 at 5:18
  • As @KrzysiekDróżdż stated, this sounds fishy. Can you please share your code?
    – phatskat
    Commented Jun 3, 2019 at 15:47

1 Answer 1

0

From the code that you shared, it follows that the one responsible for duplicating the custom field low_jobs can be the line below FeedWordPress::diagnostic().

If the key low_jobs is encountered in the loop and:

  • $value == 'under 100' then field will be updated
  • $value != 'under 100' AND $lowjobtrig == 0 then if the meta low_jobs doesn't exist, it will be created, but $lowjobtrig is always 1
  • in other cases meta low_jobs will be created, even if it already exists

So every time, the key is low_jobs and the value is different from under 100 this line

add_post_meta($postId, $key, 'OVER 100', true); 

will not be executed. Instead, this code will be executed:

FeedWordPress::diagnostic( ... );
add_post_meta($postId, $key, $value, /*unique=*/ false);  // <---
1

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.