custom taxonomy - Create heirachy of post terms from array & assign post to terms

I have a text string called $location_string which is an address in this format: 1 Victoria Street, Wellington, New Zeal

I have a text string called $location_string which is an address in this format: 1 Victoria Street, Wellington, New Zealand as well as a custom hierarchical taxonomy called location.

I am trying to achieve the following:

1) Create location terms for each of the objects in the array. Country would be the top-level term, city a child of country, and street address a child of city.

2) Assign all the terms in the array to the post set by $post_id

I realise the code below is pretty flawed- any ideas on how I could fix it? Currently it only creates street and country, as well as creating street twice:

$location_array_reversed = array_reverse( $location_array );

$i = 0;
$len = count($location_array_reversed);

$location_array_ids = array();

foreach( $location_array_reversed as $term ){

    if ($i == 0) {

        // Top level term
        wp_insert_term( $term, 'location' );
        $tag = get_term_by( 'slug', $term, 'location' );
        $term_id =  $tag->term_id;

        // Save term ID to array
        $location_array_ids[] = $term_id;

    } else if ($i == $len - 1) {

        wp_insert_term( $term, 'location', array( 'parent'=> $term_id ) );

        // Child terms
        wp_insert_term( $term, 'location' );
        $tag = get_term_by( 'slug', $term, 'location' );
        $term_id =  $tag->term_id;

        // Save term ID to array
        $location_array_ids[] = $term_id;

    }

    $i++;

}

// Now assign terms to post
wp_set_object_terms( $post_id, $location_array_ids, 'location' );

I have a text string called $location_string which is an address in this format: 1 Victoria Street, Wellington, New Zealand as well as a custom hierarchical taxonomy called location.

I am trying to achieve the following:

1) Create location terms for each of the objects in the array. Country would be the top-level term, city a child of country, and street address a child of city.

2) Assign all the terms in the array to the post set by $post_id

I realise the code below is pretty flawed- any ideas on how I could fix it? Currently it only creates street and country, as well as creating street twice:

$location_array_reversed = array_reverse( $location_array );

$i = 0;
$len = count($location_array_reversed);

$location_array_ids = array();

foreach( $location_array_reversed as $term ){

    if ($i == 0) {

        // Top level term
        wp_insert_term( $term, 'location' );
        $tag = get_term_by( 'slug', $term, 'location' );
        $term_id =  $tag->term_id;

        // Save term ID to array
        $location_array_ids[] = $term_id;

    } else if ($i == $len - 1) {

        wp_insert_term( $term, 'location', array( 'parent'=> $term_id ) );

        // Child terms
        wp_insert_term( $term, 'location' );
        $tag = get_term_by( 'slug', $term, 'location' );
        $term_id =  $tag->term_id;

        // Save term ID to array
        $location_array_ids[] = $term_id;

    }

    $i++;

}

// Now assign terms to post
wp_set_object_terms( $post_id, $location_array_ids, 'location' );
Share Improve this question asked May 16, 2019 at 20:47 warm__tapewarm__tape 611 silver badge11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Only the first and last term is added, all others are omitted in your code. A street is created twice, because you double-insert the last element of the array.

else if ($i == $len - 1) {
    wp_insert_term( $term, 'location', array( 'parent'=> $term_id ) );
    // Child terms
    wp_insert_term( $term, 'location' );
}

Try to change foreach loop:

$location_array_reversed = array_reverse( $location_array );

$parent_id = 0; 
$location_array_ids = [];
$taxonomy_slug = 'location';
foreach( $location_array_reversed as $term ) { 

    $res = term_exists( $term, $taxonomy_slug, $parent_id );
    if ( $res === NULL || $res == 0 )
        $res = wp_insert_term( $term, $taxonomy_slug, ['parent' => $parent_id] );

    $term_id =  (int) $res['term_id'];  // ID of existing or inserted term

    // Save term ID to array 
    $location_array_ids[] = $term_id; 
    $parent_id = $term_id; 
}
wp_set_object_terms($post_id, $location_array_ids, $taxonomy_slug);

wp_insert_term() returns ID of the inserted term, so you don't need to use get_term_by() to get ID of the inserted element.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745470872a4629126.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信