post meta - update_post_meta adding to array instead of replacing value

admin2025-01-07  4

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

    }

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

    }
Share Improve this question edited Jun 4, 2019 at 6:03 nmr 4,5212 gold badges17 silver badges25 bronze badges asked Jun 3, 2019 at 3:39 Benedict HarrisBenedict Harris 1213 bronze badges 2
  • 1 Update_post_meta should not do that. Could you show us your current code? – Krzysiek Dróżdż 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
Add a comment  | 

1 Answer 1

Reset to default 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);  // <---
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256497a365.html

最新回复(0)