在 wp_postmeta 中保存自定义字段 [英] Saving custom fields in wp_postmeta

查看:33
本文介绍了在 wp_postmeta 中保存自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义帖子类型在数据库中保存帖子元数据.问题是我无法存储 meta_key,唯一存储的是元值.我在那里有两个文本框,一个用于事件日期,一个用于事件名称.我试图将日期存储在元键上,并将事件存储在值上.问题是它只存储在元值上,我不确定元键.另外,您如何在同一个帖子中存储多个元键和元值?知道这个语法的哪一部分是错误的吗?谢谢:)

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

推荐答案

你为什么把它当作数组??

Why do you treat it like array ??

先试试这个简单的功能

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 .

(如果您仍然发现问题,请尝试使用 $post_id 而不是 $post->ID )

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

编辑我评论后

当你执行 update_post_meta($post->ID, $_POST["_event_date"], $_POST["_event_name"] );

您实际上是将 _event_date 保存为 KEY,将 _event_name 保存为 VALUE.

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

除非你特别清楚自己在做什么——这是错误的重点是保存2个字段.

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

一个名为 "_event_date" 来保存 dates

One named "_event_date" to hold the dates

一个名为 "_event_name" 来保存 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 .

您应该将每个表单字段(meatbox 字段)视为一组独立的 key % value - 每个都是它自己的......试试上面发布的代码.

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 .

这篇关于在 wp_postmeta 中保存自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆