0

I am trying to change default values inside array in custom field.

But I am getting error "PHP Fatal error: Uncaught Error: Cannot use string offset as an array" when I try to change the value using following code.

Strangely I use this code in default page template and it works there when I statiscally provide the post id..

but gives error for following code

add_action('pending_to_publish', 'oj_publish_campaign',10 ,1);

function oj_publish_campaign($post) {
    global $wpdb;
    $t = get_post_meta( $post->ID, '_give_donation_levels', true ); 
    $t[0]["_give_amount"] = 25.00;    // getting error here 
    $t[1]["_give_amount"] = 65.00;
    $t[2]["_give_amount"] = 100.00;
    $t[3]["_give_amount"] = 250.00;
    $t[4]["_give_amount"] = 650.00;    

    update_post_meta( $post->ID, '_give_donation_levels', $t );
}

this is how the initial array for the custom field looks like

enter image description here

And the strange thing is that it works inside a page-template i.e

if I create a page "Test" and assign it the following template and just load the page , it updates the fields as required

/*
Template Name: Test
*/
get_header();
while ( have_posts() ) : the_post();

$p = 2234;

$t = give_get_meta( $p, '_give_donation_levels', true );
echo "<pre>"; var_dump($t);echo "</pre>";

$t[0]["_give_amount"] = 25.00;
$t[1]["_give_amount"] = 65.00;
$t[2]["_give_amount"] = 100.00;
$t[3]["_give_amount"] = 250.00;
$t[4]["_give_amount"] = 650.00;

update_post_meta( $p, '_give_donation_levels', $t );        
endwhile; // End of the loop.
get_footer();

Can anyone please help me with this

thanks in advance

1 Answer 1

0

The structure of the array you've provided suggests that you're trying to access an incorrect level of the array. Try to replace:

$t[0]["_give_amount"] = 25.00;
$t[1]["_give_amount"] = 65.00;
$t[2]["_give_amount"] = 100.00;
$t[3]["_give_amount"] = 250.00;
$t[4]["_give_amount"] = 650.00;

with:

$t[0][0]["_give_amount"] = 25.00;
$t[0][1]["_give_amount"] = 65.00;
$t[0][2]["_give_amount"] = 100.00;
$t[0][3]["_give_amount"] = 250.00;
$t[0][4]["_give_amount"] = 650.00;
1
  • thanks for your reply. But the strange thing is that if I use the same code inside a page template (see updated question) by providing the static page ID then it updates the fields as required.. Infact when I used your code its not working as expected for eg: as you can see it added a new key-data in the array awesomescreenshot.com/image/… Jul 12 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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