Wordpress 自定义帖子:将 "meta_key" 限制为 "post_type"

Wordpress custom posts : restrict "meta_key" to "post_type"

我构建了一个拥有大量用户的插件。我已经根据这里和那里的示例数量 + 强大的法典构建了它。我正在使用自定义 post 类型来存储值并在前端重用它们来显示内容。

前段时间,我注意到一个可怕的行为。每次创建(常规或自定义)post 时,它都会在 WP_postmeta 中生成额外的 meta_keys。如果我停用插件,该模式就会停止。我基本上是用空值填充我的用户数据库,如此处所示,这也增加了 meta_id 的数量。不好。

我最初考虑过不存储空字段,但这不是 "right" 的做法。我如何将这些元密钥的提交限制为我的自定义 post 类型?

我的 post 类型的声明有什么可疑之处吗?

function booking_pluginbox_init() {
  $labels = array(
    'name'               => __('Booking.com Affiliate Plugin', 'post type general name'),
    'singular_name'      => __('Search Box', 'post type singular name'),
    'add_new'            => __('Add new', 'member'),
    'add_new_item'       => __('Create a new Search box'),
    'edit_item'          => __('Edit a Search box'),
    'new_item'           => __('Create a new Search box'),
    'view_item'          => null,
    'search_items'       => __('Search'),
    'not_found'          =>  __('No result found!'),
    'not_found_in_trash' => __('No Search Boxes in the trash!'),
    'parent_item_colon'  => ''
  );

  $args = array(
    'labels'               => $labels,
//    'public'               => true,
    'publicly_queryable'   => true,
    'show_ui'              => true,
    'query_var'            => true,
    'rewrite'              => array('slug' => 'booking-pluginbox'),
    'capability_type'      => 'post',
    'hierarchical'         => false,
    'has_archive'          => true,
    'menu_position'        => 100,
    'menu_icon'            => plugins_url('/includes/images/booking_plugin-icon.png', dirname(__FILE__)),
    'supports'             => array('title'),
    'register_meta_box_cb' => 'booking_pluginbox_meta_boxes'
  );

    register_post_type('booking-pluginbox',$args);
    add_action( 'save_post', 'booking_pluginbox_save_postdata' );
}
add_action('init', 'booking_pluginbox_init');

这是到 SVN 中继的 link:http://plugins.svn.wordpress.org/bookingcom-affiliate-plugin/trunk/

更新 1

当然,这是 post_save 函数

function booking_pluginbox_save_postdata($post_id) {
  if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return $post_id;
  } 

//   Check user permissions
  if ($_POST['post_type'] == 'page') {
    if (!current_user_can('edit_page', $post_id)) return $post_id;
  } else {
    if (!current_user_can('edit_post', $post_id)) return $post_id;
  }

//   OK, we're authenticated: we need to find and save the data
  $current_bp_AID = get_post_meta($post_id, 'booking_plugin_AID', false);
  $new_bp_AID = (isset($_POST['booking_plugin_AID'])) ? $_POST['booking_plugin_AID'] : '';
  $current_bp_TRACKING = get_post_meta($post_id, 'booking_plugin_TRACKING', false);
  $new_bp_TRACKING = (isset($_POST['booking_plugin_TRACKING'])) ? $_POST['booking_plugin_TRACKING'] : '';
  $current_bp_DESTINATION = get_post_meta($post_id, 'booking_plugin_DESTINATION', false);
  $new_bp_DESTINATION = (isset($_POST['booking_plugin_DESTINATION'])) ? $_POST['booking_plugin_DESTINATION'] : ''; 
...
  $current_bp_CSS_override = get_post_meta($post_id, 'booking_plugin_CSS_override', false);
  $new_bp_CSS_override = (isset($_POST['booking_plugin_CSS_override'])) ? $_POST['booking_plugin_CSS_override'] : '';
  $current_bp_jqtheme = get_post_meta($post_id, 'booking_plugin_jqtheme', false);
  $new_bp_jqtheme = (isset($_POST['booking_plugin_jqtheme'])) ? $_POST['booking_plugin_jqtheme'] : '';

  booking_pluginbox_clean($new_bp_AID);
  booking_pluginbox_clean($new_bp_TRACKING);
  booking_pluginbox_clean($new_bp_DESTINATION);
  ...
  booking_pluginbox_clean($new_bp_wpx);
  booking_pluginbox_clean($new_bp_CSS_override);
  booking_pluginbox_clean($new_bp_jqtheme);  


  if (!empty($current_bp_AID)) {
    if (is_null($new_bp_AID)) {
      delete_post_meta($post_id,'booking_plugin_AID');
    } else {
      update_post_meta($post_id,'booking_plugin_AID',$new_bp_AID);
    }
  } elseif (!is_null($new_bp_AID)) {
      add_post_meta($post_id,'booking_plugin_AID',$new_bp_AID,true);
  }

  if (!empty($current_bp_TRACKING)) {
    if (is_null($new_bp_TRACKING)) {
      delete_post_meta($post_id,'booking_plugin_TRACKING');
    } else {
      update_post_meta($post_id,'booking_plugin_TRACKING',$new_bp_TRACKING);
    }
  } elseif (!is_null($new_bp_TRACKING)) {
      add_post_meta($post_id,'booking_plugin_TRACKING',$new_bp_TRACKING,true);
  }

  if (!empty($current_bp_DESTINATION)) {
    if (is_null($new_bp_DESTINATION)) {
      delete_post_meta($post_id,'booking_plugin_DESTINATION');
    } else {
      update_post_meta($post_id,'booking_plugin_DESTINATION',$new_bp_DESTINATION);
    }
  } elseif (!is_null($new_bp_DESTINATION)) {
      add_post_meta($post_id,'booking_plugin_DESTINATION',$new_bp_DESTINATION,true);
  }

   ...

  if (!empty($current_bp_CSS_override)) {
    if (is_null($new_bp_CSS_override)) {
      delete_post_meta($post_id,'booking_plugin_CSS_override');
    } else {
      update_post_meta($post_id,'booking_plugin_CSS_override',$new_bp_CSS_override);
    }
  } elseif (!is_null($new_bp_CSS_override)) {
      add_post_meta($post_id,'booking_plugin_CSS_override',$new_bp_CSS_override,true);
  }

  if (!empty($current_bp_jqtheme)) {
    if (is_null($new_bp_jqtheme)) {
      delete_post_meta($post_id,'booking_plugin_jqtheme');
    } else {
      update_post_meta($post_id,'booking_plugin_jqtheme',$new_bp_jqtheme);
    }
  } elseif (!is_null($new_bp_jqtheme)) {
      add_post_meta($post_id,'booking_plugin_jqtheme',$new_bp_jqtheme,true);
  }  

  return $post_id;
}

谢谢指点!! 格雷格

目前,您的函数将为每个保存 post 操作 运行。在继续之前,您应该查询它是否是自定义 post 类型。另一种方法也是一个好主意是添加通知。

此外,您不需要 add_post_meta,而是使用 update_post_meta,如果它不存在,它将创建一个新的元键。

function booking_pluginbox_save_postdata($post_id) {
      if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
      } 

    // check post type

    if ($_POST['post_type'] != 'booking-pluginbox') {
        return $post_id;    
    }   


//   Check user permissions -- it was just checking if the post type was a page.
    if ($_POST['post_type'] == 'booking-pluginbox') {
        if (!current_user_can('edit_page', $post_id)) return $post_id;
    } else {
        if (!current_user_can('edit_post', $post_id)) return $post_id;
    }


    // no need to pull the old values unless you are working with them.
    if ($new_bp_AID) { // update post meta will create a new meta if non-existant. 
        update_post_meta($post_id,'_booking_plugin_AID',$new_bp_AID); // add "_"  if there should only be 1 key
    } else {
        delete_post_meta($post_id,'_booking_plugin_AID');
    }   

    return $post_id;
}