Saving custom fields in wp_postmeta

2019-09-18 08:28发布

问题:

I'm trying to save a post meta data on the database, using custom post type. The problem is I can't store the meta_key, the only one storing is the meta-value. I have two textbox there, one for event date and one for event name.. I'm trying to store the date on the meta key and the event on the value. the problem is that it only store on the meta value, and i'm not sure on the meta-key. Also how do you store multiple meta-keys and meta-values on the same post? Any idea which part of this syntax is wrong? thanks :)

function add_calendar_metaboxes() {
    add_meta_box('wpt_calendar_location', 'Event Date', 'wpt_calendar_location', 'calendar_holiday', 'normal', 'default');

}

// The Event Location Metabox
function wpt_calendar_location() {
    global $post;

    echo "<form method=\"POST\">";
    // Noncename needed to verify where the data originated
    echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    // Get the location data if its already been entered
    $event_name = get_post_meta($post->ID, '_event_name', true);
    $event_date = get_post_meta($post->ID, '_event_date', true);

    echo '<label>Event Date</label><input type="text" name="_event_date" value="' . $event_date . '" />';
    echo '<label>Event Name</label><input type="text" name="_event_name" value="' . $event_name . '" />';
    echo '<input type="submit" name="Submit">';
    echo '</form>';

}


// Save the Metabox Data
function wpt_save_events_meta($post_id, $post) {

        if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
        return $post->ID;
        }

        if ( !current_user_can( 'edit_post', $post->ID ))
            return $post->ID;

         $events_meta[] = array($_POST['_event_date'] => $_POST['_event_name']);

        foreach ($events_meta as $key => $value) 
        { 
            if( $post->post_type == 'revision' ) return; 
            $value = implode(',', (array)$value);
            if(get_post_meta($post->ID, $key, FALSE)) 
            { 
                update_post_meta($post->ID, $key, $value);
            } 

            else 
            { 
                add_post_meta($post->ID, $key, $value);
            }
            if(!$value) delete_post_meta($post->ID, $key); 
        }

}
add_action('save_post', 'wpt_save_events_meta', 1, 2);

回答1:

Why do you treat it like array ??

Try this simple function first

function wpt_save_events_meta() {
 global $post;

 update_post_meta($post->ID, "_event_date", $_POST["_event_date"]);
 update_post_meta($post->ID, "_event_name", $_POST["_event_name"]);
}

If this is working for you, then you can put all your validation stuff .

( IF you still find a problem, try $post_id instead of $post->ID )

EDIT I after comment

When you do update_post_meta($post->ID, $_POST["_event_date"], $_POST["_event_name"] );

You are actually saving the _event_date as KEY and the _event_name as VALUE.

Unless you specifically know what you are doing - this is wrong The point is to save 2 fields.

One named "_event_date" to hold the dates

One named "_event_name" to hold the names

In the case you wrote in the comments you will have something like ( I do not know the real data structure ..)

( post_id=1 ) 2014.03.04 => my_event_name
( post_id=2 ) 2014.03.05 => my_event_name2
( post_id=3 ) 2014.03.06 => my_event_name3

In the correct code you will have

( post_id=1 ) _event_date => 2014.03.04
              _event_name => my_event_name
( post_id=2 ) _event_date => 2014.03.05
              _event_name => my_event_name2
( post_id=3 ) _event_date => 2014.03.06
              _event_name => my_event_name3

You can see that in the first code , it will be really hard to actually use the data in a search for example as in the second code - it has a structure .

You should treat each form field ( meatbox field ) as an independent set of key % value - each by it´s own.. Just try the code posted above .



标签: php wordpress