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

时间:2015-01-30 19:22:35

标签: wordpress custom-post-type custom-taxonomy

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

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

empty keys filling wp_postmeta

我最初想过不存储空字段,但这不是“正确”的做法。如何限制将这些元数据提交到我的自定义帖子类型?

我的帖子类型声明有什么可疑的吗?

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中继的链接:http://plugins.svn.wordpress.org/bookingcom-affiliate-plugin/trunk/

UPDATE1

当然,这是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;
}

感谢您的指示!! 格雷格

1 个答案:

答案 0 :(得分:1)

此时,您的功能将针对每个保存帖子操作运行。在继续之前,您应该查询它是否是自定义帖子类型。另一种方式也是一个好主意是添加一个nounce。

此外,您不需要使用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;
}