我正在尝试设置自定义帖子类型以下 本教程. 。但是,我有点困惑,就像如何/在哪里实施 update_post_meta(). 。教程提出了这种模式:

add_action('save_post', 'save_my_metadata');

function save_my_metadata()
{
    global $post;
    update_post_meta($post->ID, 'my_metadata', $_POST['my_metadata']);
}

确实有效,但不幸地将元数据添加到每个帖子中,无论它是否属于此自定义类型。

我把以上的 functions.php 并且猜测这可能是问题的一部分。我猜想我需要限制“ save_post”操作以仅触发自定义类型的帖子。

有帮助吗?

解决方案

function save_my_metadata($ID = false, $post = false)
{
    if($post->post_type != 'your_post_type')
        return;
    update_post_meta($ID, 'my_metadata', $_POST['my_metadata']);
}

那应该起作用。只需将'your_post_type'替换为帖子类型的名称即可。另外,鲜为人知的事实:“ save_post”挂钩将帖子的ID作为参数传递。

编辑

我更新了该功能以反映Jan的评论。谢谢扬!

其他提示

如果要处理多种帖子类型,我建议您使用基本的开关语句:

add_action('save_post', 'save_my_metadata');

function save_my_metadata($ID = false, $post = false)
{
    switch($post->post_type) 
    {
        case 'post_type_1':
            // Do stuff for post type 1
            update_post_meta($ID, 'my_metadata', $_POST['my_metadata']); // Example...
            break;
        case 'post_type_2':
            // Do stuff for post type 2
            break;
        default:
            return;
    }
}

案例基本上与 if($post->post_type) == 'post_type_1') {} 但不需要多个IF-ELSE块。这 default 在开关中,块处理帖子类型不在您的自定义集中的情况下。

@john P Bloch和@eamann已经给出了很好的答案,因此我的补充是:

  1. 考虑 用下划线将您的meta_keys前缀. 。这样做将它们隐藏在邮政编辑屏幕上显示的自定义字段列表中,即

    function save_my_metadata($post_id,$post=false) {
       if($post->post_type=='your_post_type')
          update_post_meta($post_id, '_my_metadata', $_POST['my_metadata']);
    }
    
    显然,这意味着您也需要一个自定义的Metabox来编辑字段。这是上下文的编辑屏幕:



  2. 您可以做的另一件事是添加自己的钩子以使保存特定的帖子类型更容易,即您的钩子可能是”save_{$post_type}_post“; 为一个 movie 帖子类型是 save_movie_post. 。这是您必须添加到主题的 functions.php 文件或插件中的某个地方:

    add_action('save_post', 'save_custom_post_type_posts',10,2);
    function save_custom_post_type_posts($post_id,$post=false) {
       do_action("save_{$post->post_type}_post");
    }
    
    然后,您可以像这样重写原始代码(包括上面#1的下划线技巧):

    add_action('save_my_postype_post','save_my_postype_metadata',10,2);
    function save_my_postype_metadata($post_id,$post) {
        update_post_meta($post_id, '_my_metadata', $_POST['my_metadata']);
    }

就个人而言,我更喜欢遵循以下模式,以添加自定义的元处理程序为发布类型。使用以下内容,您可以通过调用add_post_type_support('my_post_type','subtitle');您可以通过将支持键(在下面的示例中添加'subtitle'(在下面的示例中)添加到支持数组的支持数组来添加元支持。

class Subtitle_Meta_Handler {
    public function initialize() {
        add_action('add_meta_boxes', array($this, 'add_metabox'), 10, 2);
        add_action('save_post', array($this, 'update'));
    }

    public function add_metabox($post_type, $post)
    {
        if(post_type_supports($post_type, 'subtitle'))
        {
            add_meta_box('subtitle', 'Subtitle', array($this, 'metabox'), $post_type);
        }
    }

    public function metabox($post)
    {
        $subtitle = get_post_meta($post->ID, 'subtitle', true);
        if(!$subtitle)
        {
            $subtitle = '';
        }
        ?>
        <input type="text" style="width: 70%;" value="<?php echo esc_attr($subtitle);?>" name="subtitle" id="subtitle">
        <?php
        wp_nonce_field('update_subtitle', 'subtitle_nonce');
    }

    public function update($post_id)
    {
        if(wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) {
            return $post_id;
        }
        if(isset($_REQUEST['subtitle_nonce']) && wp_verify_nonce($_REQUEST['subtitle_nonce'], 'update_subtitle')) {
            $subtitle = trim(strip_tags($_REQUEST['subtitle'], '<b><strong><span><a>'));
            if(empty($subtitle)) {
                delete_post_meta($post_id, 'subtitle');
            } else {
                update_post_meta($post_id, 'subtitle', $subtitle);
            }
        }
    }
}
add_action('init', array(new Subtitle_Meta_Handler(), 'initialize'));

希望这样的东西很快将被添加到核心中。

事先更新,请检查当前帖子是否是您的后类型的。这将确保您不会为所有帖子保存它。

您也应该检查输入(示例中缺少),然后请记住,只有在该型后类型处于活动状态时,您才能添加操作。如果是这种情况,则您无需以后检查该后类型。

获取帖子类型: get_post_type() 或者 $post->post_type;

我不能让它工作 - 不确定我在做什么错 - 但是我尝试使用post_updated钩子而不是save_post-因为我希望在更新帖子后插入这些值,以便我可以从其他自定义字段中检索值。

 function update_meta ($ID = false, $post = false) {
  update_post_meta($ID, 'rest_long', 'Test 1');
  update_post_meta($ID, 'rest_lat', 'Test 2');
}

add_action('post_updated', 'update_meta');
许可以下: CC-BY-SA归因
scroll top