查询以在WordPress中添加分类术语


Query to add taxonomies terms in WordPress

我使用JSON API插件进行wordpress,可以将信息添加到任何自定义帖子类型的元框(自定义字段)中,但不能对分类法进行同样的操作。

下面的元框(自定义字段)代码:

// For prettier URLs, a map of post_type => (url_param_name => meta_field_name)
$CUSTOM_POST_META_FIELDS = array(
'classifieds' => array(
'price' => 'wpcf-price-with-discount',
'condition' => 'wpcf-condition'
)
);
// Handle metadata
global $CUSTOM_POST_META_FIELDS;
if ($this->id && !empty($CUSTOM_POST_META_FIELDS[$wp_values['post_type']])) {
  foreach ($CUSTOM_POST_META_FIELDS[$wp_values['post_type']] as $param => $meta) {
    update_post_meta($this->id, $meta, $values[$param]);
  }
}

你能建议添加分类术语的类似代码吗?例如,post-type"classifieds"有一个分类法"type",我想添加任何术语。

另外一个想法是在//处理元数据之后以某种方式修改代码的第二部分

 function set_custom_taxonomies($type) {
global $json_api;
$taxonomies = get_taxonomies(array(
  'object_type' => array($type),
  'public'   => true,
  '_builtin' => false
), 'objects');
foreach ($taxonomies as $taxonomy_id => $taxonomy) {
  $taxonomy_key = "taxonomy_$taxonomy_id";
  if (!$json_api->include_value($taxonomy_key)) {
    continue;
  }
  $taxonomy_class = $taxonomy->hierarchical ? 'JSON_API_Category' : 'JSON_API_Tag';
  $terms = get_the_terms($this->id, $taxonomy_id);
  $this->$taxonomy_key = array();
  if (!empty($terms)) {
    $taxonomy_terms = array();
    foreach ($terms as $term) {
      $taxonomy_terms[] = new $taxonomy_class($term);
    }
    $this->$taxonomy_key = $taxonomy_terms;
  }
}
 }

提前谢谢!

当我通过JSON API发布自定义帖子类型时,我使用以下内容来设置特定类别。可以修改为从$values获取数组,然后获取所有术语的id数组。

在controller/posts.php 的函数save()中放置以下内容

内部:

if (isset($wp_values['ID'])) {

以下两者:

$this->id = wp_update_post($wp_values);
$this->id = wp_insert_post($wp_values);
//First get the id of the category(custom taxonomy) you want this post to appear. I
$term = term_exists( 'slug', 'type' ); //returns term's id if it exists
wp_set_post_terms($this->id,$term,'your_custom_taxonomy');