Vegas Best Awards Business Terms Migration


//Code to Export Business Terms and terms to migrate too
$posts = get_posts(array(
    'post_type'     => 'u',
    'numberposts' => -1
));
$count = count($posts);

echo "
<h3>Export Business Categories</h3>
<span class='total'>Total Users: {$count}</span>
<table>
    <tr>
        <th>Business ID</th>
        <th>Business Name</th>
        <th>User Tags</th>
        <th>Categories</th>
    </tr>";
    
foreach($posts as $post) {
    $post_id = $post->ID;
    $name = $post->post_title;
    $terms = get_the_terms($post_id, 'ut');
    $termIDs = wp_list_pluck($terms, "term_id");
    $term_ids_strings = implode(", ",$termIDs);
    $termNames = wp_list_pluck($terms, "name");
    $term_names_string = implode(", ", $termNames);
    
    $newtermList = [];
    foreach($termNames as $t_name) {
        $newTerm = get_term_by('name', $t_name, 'category');
        if($newTerm == false) { continue; }
        
        if($newTerm->parent != 0) {
            $parentName = get_term_by('ID', $newTerm->parent, 'category');
            $string = $parentName->name.">".$newTerm->name;
            
            if($parentName->parent != 0) {
                $parentName2 = get_term_by('ID', $parentName->parent, 'category');
                $string = $parentName2->name.">".$parentName->name.">".$newTerm->name;
                array_push($newtermList, $string);
                continue;
            }
            
            array_push($newtermList, $string);
            continue;
        }
        array_push($newtermList, $newTerm->name);
    }
    $new_terms_string = implode(", ", $newtermList);
    
    echo "
    <tr>
        <td>{$post_id}</td>
        <td>{$name}</td>
        <td>{$term_names_string}</td>
        <td>{$new_terms_string}</td>
    </tr>
    ";
}
echo "</table>";

Did you find this article useful?