简体   繁体   English

PHP将WP条款子条款导入自定义帖子类型

[英]PHP Import WP Terms child term into custom post type

I have created a import tool to import data from a json file to create posts in my custom post type. 我创建了一个导入工具,用于从json文件导入数据,以自定义帖子类型创建帖子。 It all works well and I can import acf fields as well as assigned terms. 一切正常,我可以导入acf字段以及指定的条款。 My question is, how can I import child term assigned to the term. 我的问题是,如何导入分配给该术语的子术语。

I have two variables right now that collects the data. 我现在有两个变量来收集数据。 I would like the have the variable eventCategoryChildName to assign its vaule as a child term to eventCategoryID 我想让变量eventCategoryChildName将其有效值作为子项分配给eventCategoryID

  $eventCategoryChildName = $ev['OfficialCity'];
  $eventCategoryId = $ev['RegionID'];

Here is how the import of terms functions right now without child terms: 这是现在没有子术语的情况下术语导入的功能:

 // set category terms
 $cat_ids = $eventCategoryName;
 // Add these categories, note the last argument is true.
 wp_set_object_terms( $post_id, $cat_ids, 'lan', true );

Edit: 编辑:

So I managed to import the child associated to the right parents but they are not checked to the post 因此,我设法将与正确的父母相关联的孩子导入,但未将其选中

    // set category terms
    $cat_ids = $eventCategoryName;

    // Import category name
    $term_taxonomy_ids = wp_set_object_terms( $post_id, $cat_ids, 'lan', true );

    $parent_term = wp_set_object_terms( $post_id, $cat_ids, 'lan', true );

    // Check terms import for errors
    if ( is_wp_error( $term_taxonomy_ids ) ) {
        // There was an error somewhere and the terms couldn't be set.
        echo $return->get_error_message();
    } else {
        // Success! These categories were added to the post.
    }


    $parent_term = term_exists( $eventCategoryName, 'lan' ); 
    wp_insert_term(
      $eventCategoryChildName,                // Customize as you wish
      'lan',
      array(
          'parent'      => $parent_term['term_id'],
          'slug'        => $eventCategoryChildName  // Customize as you wish
      )
    );

    if ( !is_wp_error( $child_term_result ) ) {
      wp_set_object_terms( $post_id, $child_term_result['term_id'], 'lan', true );
  }

Firstly, you should check the result of wp_set_object_terms , to see if an error was returned: 首先,您应该检查wp_set_object_terms的结果,以查看是否返回了错误:

$term_taxonomy_ids = wp_set_object_terms( $post_id, $cat_ids, 'lan', true );
if ( is_wp_error( $term_taxonomy_ids ) ) {
    // There was an error somewhere and the terms couldn't be set.
    echo $return->get_error_message();
} else {
    // Success! These categories were added to the post.
}

Then, the term you want to add may not exists. 然后,您要添加的术语可能不存在。 In such case, wp_set_object_terms will return an invalid_taxonomy error. 在这种情况下, wp_set_object_terms将返回invalid_taxonomy错误。 So you may want to add such missing term to your taxonomy: 因此,您可能需要在分类法中添加以下缺失术语:

wp_insert_term(
    'Term Name',                      // Customize as you wish
    'lan',
    array(
        'description' => 'Term desc', // Customize as you wish
        'slug'        => 'lan-slug1'  // Customize as you wish
    )
);

// For child term:
$parent_term = term_exists( 'parent_term_id', 'lan' );
$child_term_result = wp_insert_term(
    'Child Term Name',                // Customize as you wish
    'lan',
    array(
        'parent'      => $parent_term['term_id']
        'description' => 'Term desc', // Customize as you wish
        'slug'        => 'lan-slug1'  // Customize as you wish
    )
);

// Add the child term to the post
if ( !is_wp_error( $child_term_result ) ) {
     wp_set_object_terms( $post_id, $child_term_result['term_id'], 'lan', true );
}

Finally, be sure you execute this code after -at least- the init hook of Wordpress. 最后,请确保至少在Wordpress的init钩子之后执行此代码。 For instance: 例如:

function parse_my_json() {
    // Your code here
}
add_action('init','parse_my_json');

Useful references: 有用的参考资料:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM