更新查询中岗位类型的分类值

时间:2015-08-11 作者:Maurice

我的情况是,在一个分类类别(国家)的自定义帖子类型(旅行)中,我有大约5000条导入的记录。由于我从几个不同的地方导入,每个来源似乎对一些国家使用了不同的描述,因此我想将该帖子的术语引用更新为统一的描述。

因此,分类学“国家”具有以下价值:


+---------+-------------+
| term_id | name        |
+---------+-------------+
|  1248   | Zuid Afrika |
+---------+-------------+
|  3845   | zuid-afrika |
+---------+-------------+
我想将每个“旅行”帖子中重复的“国家”一词更新为新定义的id。


$terms = array(
    \'3845\' => \'1248\'
);
$args = array(
    \'posts_per_page\'    => -1,
    \'post_type\'         => \'travel\',
    \'post_status\'       => \'publish\'
);
global $post;
$my_query = new WP_Query($args);
if($my_query->have_posts()){
    while ($my_query->have_posts()) : $my_query->the_post();
        /*
            This is the part where i get lost, what i want is something like this, but this doesn\'t work
        */
        if(in_array($post->term_id,$terms){
            update term reference with value $terms[$post->term_id];
        }
    }
    endwhile;
    wp_reset_query();
}
我非常感谢你的帮助,因为我现在很困

Edit:我不想删除分类法类别,因为此脚本将在导入之后运行,因此如果删除重复项,它们将直接导入,但使用不同的术语id

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

不幸的是,webtoure的解决方案不起作用,因此我决定深入研究Wordpress的数据库结构,并提出以下工作代码:


global $wpdb;
$taxonomy = \'country\';
$environment = 0;

function _get_term_taxonomy_id($term_id){
    global $wpdb;
    $results = $wpdb->get_results("
        SELECT tt.term_taxonomy_id
        FROM wp_term_taxonomy AS tt
        LEFT JOIN wp_terms AS t ON t.term_id = tt.term_id
        WHERE t.term_id = \'$term_id\'
    ");
    foreach($results as $result){
        return $result->term_taxonomy_id;
    }
}

/* MERGE */
foreach($merge_map as $from => $to){
    $from_id = _get_term_taxonomy_id($from);
    $to_id = _get_term_taxonomy_id($to);
    if($environment == 1){
        $wpdb->update( 
            \'wp_term_relationships\', 
            array( 
                \'term_taxonomy_id\' => $to_id
            ), 
            array( \'term_taxonomy_id\' => $from_id )
        );
        echo \'UPDATE \'.$from_id.\' with \'.$to_id.\' DONE
\';
    }
}

/* UPDATE PARENT */
foreach($parent_map as $term_id => $parent_id){
    if($environment == 1){
        $wpdb->update( 
            \'wp_term_taxonomy\', 
            array( 
                \'parent\' => $parent_id
            ), 
            array( \'term_id\' => $term_id )
        );
        echo \'UPDATE parent of \'.$term_id.\' with \'.$parent_id.\' DONE
\';
    }
}

SO网友:webtoure

这里涉及的步骤相当多,而且,您提到的5000篇帖子,这将是一项昂贵的任务,通过WordPress本身来完成。然而,以下是我想到的(I haven\'t tested this so you may wish to use it on a backup first and see if it works):

global $wpdb;

/* The ( \'from_term_id\', \'to_term_id\' ) map. */
$terms_map = array(
    \'3845\' => \'1248\',
    /* ... The rest of your mapping */
);

$taxonomy = \'country\';
$obj_terms = wp_get_object_terms( $post->ID, $taxonomy );

/* Loop through each object term */
foreach ( $obj_terms as $term ) {
    /* See if the obj term_id is a key in $terms_map */
    if ( isset( $terms_map[$term->term_id] ) ) {
        /* We have a valid key. We now need the term_taxonomy_ids */
        /* for both \'from_term_id\' and \'to_term_id\' */
        $to_term = get_term( $terms_map[$term->term_id], $taxonomy );

        $from_term_tax_id = $term->term_taxonomy_id;
        $to_term_tax_id = $to_term->term_taxonomy_id;

        /* Update the \'{prefix}_term_relationships\' table */
        $update_res = $wpdb->update( 
            $wpdb->term_relationships, /* The table to update */
            array( \'term_taxonomy_id\' => $to_term_tax_id ), /* Data to be updated */
            array( \'object_id\' => $post->ID, \'term_taxonomy_id\' => $from_term_tax_id ), /* Where clause */
            array( \'%d\' ), /* Format of the data is int */
            array( \'%d\', \'%d\' ) /* Format of the where clause is int */
        );

        /* Finally, you may wish to update the term count for each term */
        wp_update_term_count( array( $from_term_tax_id, $to_term_tax_id ), $taxonomy );
    }
}
正如我所提到的,虽然这可能证明成本太高,但在这种情况下,您必须编写单独的PHP脚本并直接在数据库上工作。

SO网友:vancoder

如果我正确理解了您的需求,那么最简单的方法就是使用Term Management Tools 插件。导入后,可以在管理界面中重新组织术语。

编辑:原来我没有正确理解您的要求:)

结束