我试图将CPT中的一组帖子转换为自定义分类法中的项目,其中帖子标题成为分类法名称,帖子内容成为分类法描述。我有以下代码(also here) 在插件中,但不确定在哪里添加我的CPT和税务名称。或者代码是否能从插件中正确触发。有什么想法吗?
/**
* Create slugs/terms in jjm_author_tax taxonomy for all jjm_authors posts
*
*/
function make_taxonomy_from_posts($post_type, $taxonomy){
// Get all posts
$query_posts = query_posts(array(
// ... of the requested type
\'post_type\'=> $post_type,
// ... and it supports the taxonomy
\'taxonomy\' => $taxonomy,
// ... without limit
\'nopaging\' => true,
));
// Reset the main query
wp_reset_query();
foreach ($query_posts as $query_post) {
$post_id = $query_post->ID;
$raw_title = $query_post->post_title;
// Generate a slug based on the title.
// We want to check for auto-draft so we don\'t create a term for a post with no title
$slug_title = sanitize_title($raw_title);
// Do the checks for empty titles
// If the title is blank, skip it
if ($slug_title == \'auto-draft\' || empty($raw_title)) continue;
// Get all of the terms associated with the post
$terms = get_the_terms($post_id, $taxonomy);
$term_id = 0;
if (!empty($terms)) {
$term_id = $terms[0]->term_id;
}
if ($term_id > 0) {
// If the post has a term, update the term
wp_update_term($term_id, $taxonomy, array(
\'description\' => $raw_title,
\'slug\' => $raw_title,
\'name\' => $raw_title,
));
} else {
// Otherwise add a new term
wp_set_object_terms($post_id, $raw_title, $taxonomy, false);
}
}
}
最合适的回答,由SO网友:Josh M 整理而成
这是对我有效的解决方案。我将下面的代码添加到页面模板(page test.php),然后创建了一个名为;测试,“测试”;加载页面,所有CPT都转换为分类法。不确定这是在我创建页面时还是在加载页面时发生的。。。然后我迅速删除了页面模板,这样我就不会得到一堆重复的页面模板。
<?php
function cpt_to_tax($post_type, $taxonomy) {
$posts = get_posts(array(
\'post_type\' => $post_type,
\'posts_per_page\' => -1,
));
foreach($posts as $post) {
wp_insert_term($post->post_title, $taxonomy, array(
\'description\' => $post->post_content,
));
}
}
cpt_to_tax(\'jjm_authors\', \'jjm_author_tax\');
?>
解决方案本身由
https://wordpress.stackexchange.com/users/35470/circuuz, 虽然他的帖子现在似乎被删除了(
SO网友:David Gard
首先,我们创建一个新的自定义查询,其中包含类型为的所有已发布帖子$post_type
, 传递给函数。如果你不只是想要发布的帖子,你可以选择你需要的状态-see this part of the WP_Query Codex.
接下来我们使用The Loop 循环遍历结果帖子,并针对每个帖子检查帖子名称是否作为$taxonomy中的一个术语存在(您也可以将其传递给函数),使用term_exists()
, 如果没有,我们使用wp_insert_term()
. 使用这意味着我们只需传递名称和描述,而slug将根据名称自动创建。
最后,在退出之前The Loop 我们重置了全局$post
使用wp_reset_postdata()
将整个代码块放入functions.php 按照这里的顺序。而您可以让此代码在每次页面加载时运行(重复的类别名称will not ,建议出于性能方面的考虑,您应该确保对调用进行注释add_action()
完成后。
add_action(\'init\', \'init_make_taxonomy_from_posts\');
function init_make_taxonomy_from_posts(){
make_taxonomy_from_posts(\'jjm_authors\', \'jjm_author_tax\');
}
/**
* Create slugs/terms in jjm_author_tax taxonomy for all jjm_authors posts
*/
function make_taxonomy_from_posts($post_type, $taxonomy){
/** Grab the relevant posts */
$args = array(
\'post_status\' => \'publish\',
\'post_type\' => $post_type,
\'posts_per_page\' => -1
);
$my_query = new WP_Query($args);
/** Check that some posts were found and loop through them */
if($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post();
/** Check to see if a term with the same title as the post currently exists in $taxonomy */
if(!term_exists(get_the_title(), $taxonomy)) : // It doesn\'t
/** Add a new term to $taxonomy */
$args = array(
\'description\' => get_the_content()
);
wp_insert_term(get_the_title(), $taxonomy);
endif;
endwhile;
/** Reset the \'$post\' global */
wp_reset_postdata();
endif;
}
注意:此代码经过测试
100% 工作