• Resolved imjscn

    (@imjscn)


    I have an array saved in postmata, each array key become a metakey. I want to change the code to save the whole array with one metakey. How to do that?
    Here’s my current code:

    $poddata = Array(
    'pod_id' => $this->pod_id,
    'url' => $this->url,
    'name' => $this->name,
    'description' => $this->description,
    'service' => $this->service,
    'status' =>$this->status,
    'price' => $this->price
    );
    
    foreach ( $poddata as $k => $v ){
    
    if ( get_post_meta( $this->id, $k ) == '' )
    add_post_meta( $this->id, $meta_box, $v, true );
    
    elseif ( $v != get_post_meta( $this->id, $k, true ) )
    update_post_meta( $this->id, $k, $v );
    
    elseif ( $v == '' )
    delete_post_meta( $this->id, $k, get_post_meta( $this->id, $k, true ) );
    
    }

    I tried
    update_post_meta( $this->id, ‘pod’, $poddata );
    It doesn’t work.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter imjscn

    (@imjscn)

    Nevermind, it’s solved.
    update_post_meta( $this->id, ‘pod’, $poddata );
    This works. I didn’t see it in Custom Field Table. Now I just found it in phpAdmin.

    It probably is working..?

    If $poddata is an array, then WordPress will serialise it. when you call get_post_meta() make sure you pass the $single=true arg so it knows to fetch the single serialised meta value. It will then unserialise it into an array.

    What does get_post_meta($this->id, 'pod', 'single'); return?

    Thread Starter imjscn

    (@imjscn)

    @tcbarrett , thanks for reply!

    you are right, I can’t get the data.
    if $single = false, var_dump shows “array(0)”
    if $single = true, var_dump shows “string(0)”

    I want to retrive the whole array, how?

    Thread Starter imjscn

    (@imjscn)

    @tcbarrett , the “true” works! Thanks!
    Previously it didn’t work because I added “true” on save, but I didn’t add “true” to the section where it populate the data for old posts.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to save an array with one metakey in postmeta?’ is closed to new replies.