将选定列表值(分类)保存在wp中:wp_set_Object_Terms

时间:2014-09-01 作者:brabertaser19

在我的管理部分,我有其他字段(如价格或品牌-这是分类法)。编辑或创建新零件时,我会设置其他数据。

价格节约没有任何问题,但从选择列表中保存值有些奇怪-它没有保存:

wp_set_object_terms($post_id, $_POST[\'part_brand\'], \'brands\', true);
根据wp doc:此函数从分类法选择更新值。但对我来说,这不起作用。

您可以在此处看到的所有代码:

http://pastebin.com/N4gZL3uN

如何在wp中保存选择列表(分类法)中的值?

完整代码:

<?php
/*
Plugin Name: parts
Plugin URI: http://wp.tutsplus.com/
Description: descr
Version: 1.0
Author: Me
Author URI: http://wp.tutsplus.com/
License: GPLv2
*/
add_action(\'init\', \'part_init\');

function part_init()
{
  $args = array(
    \'label\' => __(\'Parts\') ,
    \'labels\' => array(
      \'edit_item\' => __(\'Edit Part\') ,
      \'add_new_item\' => __(\'Add New Part\') ,
      \'view_item\' => __(\'View Part\') ,
    ) ,
    \'singular_label\' => __(\'Part\') ,
    \'public\' => true,
    \'show_ui\' => true,
    \'_builtin\' => false,
    \'capability_type\' => \'post\',
    \'hierarchical\' => false,
    \'rewrite\' => array(
      "slug" => "parts"
    ) , // формат ссылок
    \'supports\' => array(
      \'title\',
      \'editor\',
      \'thumbnail\'
    )
  );
  register_post_type(\'part\', $args);
  $labels_gr = array(
    \'name\' => \'Группы\',
    \'singular_name\' => \'Группы\',
    \'search_items\' => \'Поиск групп\',
    \'all_items\' => \'Все группы\',
    \'parent_item\' => \'Родительская группа\',
    \'parent_item_colon\' => \'Родительская группа:\',
    \'edit_item\' => \'Редактирование группы\',
    \'update_item\' => \'Обновить группу\',
    \'add_new_item\' => \'Добавить новую группу\',
    \'new_item_name\' => \'Имя новой группы\',
    \'menu_name\' => \'Группы\',
  );
  $labels_brands = array(
    \'name\' => \'Бренды\',
    \'singular_name\' => \'Бренд\',
    \'search_items\' => \'Поиск брендов\',
    \'popular_items\' => \'Популярные бренды\',
    \'all_items\' => \'Все бренды\',
    \'edit_item\' => \'Редактировать бренд\',
    \'update_item\' => \'Обновить бренд\',
    \'add_new_item\' => \'Добавить новый бренд\',
    \'new_item_name\' => \'Имя нового бренда\',
    \'menu_name\' => \'Бренды\',
    \'separate_items_with_commas\' => \'Разделять бренды запятыми\',
    \'add_or_remove_items\' => \'Добавить или удалить бренд\',
    \'choose_from_most_used\' => \'Выберите бренд из наиболее используемых\',
    \'menu_name\' => \'Бренды\',
  );
  register_taxonomy(\'groups\', \'post\', array(
    \'hierarchical\' => true,
    \'labels\' => $labels_gr,
    \'show_ui\' => true,
    \'show_admin_column\' => true,
    \'query_var\' => true,
    \'rewrite\' => array(
      \'slug\' => \'group\'
    ) ,
  ));
  register_taxonomy(\'brands\', \'post\', array(
    \'hierarchical\' => false,
    \'labels\' => $labels_brands,
    \'show_ui\' => true,
    \'show_admin_column\' => true,
    \'update_count_callback\' => \'_update_post_term_count\',
    \'query_var\' => true,
    \'rewrite\' => array(
      \'slug\' => \'brands\'
    ) ,
  ));
}

function part_add_meta_box()
{
  add_meta_box(\'part_additional_fields\', __(\'Дополнительные поля\', \'part_textdomain\') , \'part_meta_box_callback\', \'part\');
}

add_action(\'add_meta_boxes\', \'part_add_meta_box\');

function part_meta_box_callback($post)
{
  wp_nonce_field(\'part_meta_box\', \'part_meta_box_nonce\');
  $part_weight = get_post_meta($post->ID, \'part_weight\', true);

  // $part_brand = get_post_meta( $post->ID, \'part_brand\', true );

  $part_brand = get_terms(\'brands\', \'hide_empty=0\');
  $part_price = get_post_meta($post->ID, \'part_price\', true);
  $part_group = get_terms(\'groups\', \'hide_empty=0\');
  echo \'<label for="part_weight">\';
  _e(\'Вес\', \'myplugin_textdomain\');
  echo \'</label> \';
  echo \'<input id="part_weight" name="part_weight" value="\' . esc_attr($part_weight) . \'" size="25" type="number" />\';
  echo "<br />";
?>
  <label for="part_brand">Бренд</label>
  <select name=\'part_brand\' id=\'part_brand\'>
          <option value=\'\'
          <?php
  if (!count($part_brand)) echo "selected"; ?>>None</option>
          <?php
  foreach($part_brand as $brand)
  {
    if (!is_wp_error($part_brand) && !empty($part_brand) && !strcmp($brand->slug, $part_brand[0]->slug)) echo "<option value=\'" . $brand->slug . "\' selected>" . $brand->name . "</option>\\n";
    else echo "<option value=\'" . $brand->slug . "\'>" . $brand->name . "</option>\\n";
  }

?>
  </select>    
  <?php
  echo "<br />";
  echo \'<label for="part_price">\';
  _e(\'Цена\', \'myplugin_textdomain\');
  echo \'</label> \';
  echo \'<input id="part_price" name="part_price" value="\' . esc_attr($part_price) . \'" size="25" type="number" />\';
  echo "<br />";
?>
  <label for="part_group">Группа</label>
  <select name=\'part_group\' id=\'part_group\'>
          <option value=\'\'
          <?php
  if (!count($part_group)) echo "selected"; ?>>None</option>
          <?php
  foreach($part_group as $brand)
  {
    if (!is_wp_error($part_group) && !empty($part_group) && !strcmp($brand->slug, $part_group[0]->slug)) echo "<option value=\'" . $brand->slug . "\' selected>" . $brand->name . "</option>\\n";
    else echo "<option value=\'" . $brand->slug . "\'>" . $brand->name . "</option>\\n";
  }

?>
  </select>    
  <?php
}

function part_save_meta_box_data($post_id)
{
  if (!isset($_POST[\'part_meta_box_nonce\']))
  {
    return;
  }

  if (!wp_verify_nonce($_POST[\'part_meta_box_nonce\'], \'part_meta_box\'))
  {
    return;
  }

  if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE)
  {
    return;
  }

  if (isset($_POST[\'post_type\']) && \'part\' == $_POST[\'post_type\'])
  {
    if (!current_user_can(\'edit_page\', $post_id))
    {
      return;
    }
  }
  else
  {
    if (!current_user_can(\'edit_post\', $post_id))
    {
      return;
    }
  }

  if (!isset($_POST[\'part_weight\']) || !isset($_POST[\'part_weight\']) || !isset($_POST[\'part_weight\']) || !isset($_POST[\'part_group\']))
  {
    return;
  }

  $part_weight_data = sanitize_text_field($_POST[\'part_weight\']);

  // $part_brand_data = sanitize_option( $_POST[\'part_brand\'], $_POST[\'part_brand\']);

  wp_set_object_terms($post_id, $_POST[\'part_brand\'], \'brands\', true);
  $part_price_data = sanitize_text_field($_POST[\'part_price\']);

  // $part_group = sanitize_option( $_POST[\'part_group\'] );

  wp_set_object_terms($post_id, $_POST[\'part_group\'], \'groups\', true);

  // Update the meta field in the database.

  update_post_meta($post_id, \'part_weight\', $part_weight_data);

  // update_post_meta( $post_id, \'part_brand\', $part_brand_data );

  update_post_meta($post_id, \'part_price\', $part_price_data);

  // update_post_meta( $post_id, \'part_group\', $part_group );

}

add_action(\'save_post\', \'part_save_meta_box_data\');
?>

1 个回复
最合适的回答,由SO网友:brabertaser19 整理而成

solved it so:

$part_brand = get_terms(\'brands\', \'hide_empty=0\');
<label for="part_brand">Бренд</label>
  <select name=\'part_brand\' id=\'part_brand\'>
          <option value=\'\'
          <?php
  **$brand_names = wp_get_object_terms($post->ID, \'brands\');**
  if (!count($brand_names)) echo "selected"; ?>>None</option>
          <?php
  foreach($part_brand as $brand)
  {
    if (!is_wp_error($brand_names) && !empty($brand_names) && !strcmp($brand->slug, $brand_names[0]->slug)) echo "<option value=\'" . $brand->slug . "\' selected>" . $brand->name . "</option>\\n";
    else echo "<option value=\'" . $brand->slug . "\'>" . $brand->name . "</option>\\n";
  }

?>
  </select>
结束

相关推荐

GET_POST()的PHP内存友好替代方案

我遇到了以下内存问题:get_posts( array( \'posts_per_page\' => \'-1\', \'post_type\' => array( \'product\', \'product_variation\' ) )(有数千个结果)只是尝试获取所有帖子ID的列表。下面的代码一次捕获10个产品,希望避免任何内存限制。它似乎工作正常,但可能有一个更优雅的解决方案。https://gist.github.com/dtbaker/acd15e542d98bff68034$pr