WordPress功能:创建一个与帖子名称同名的术语,但也应该为术语分配帖子

时间:2016-12-27 04:06:17

标签: wordpress function taxonomy posts

我需要功能,以便在创建帖子时,还会创建具有相同帖子名称的确切TAX中的术语。

此外,我需要自动将该帖子分配给同一个词。

我该怎么做?以下是我尝试过的功能:

function add_new_term_clubes() {
    $args = array(
        'posts_per_page'    =>  -1,
        'post_type'         =>  'clubes'
    );
    $title_query = get_posts('numberposts=-1&post_type=clubes');

    foreach ($title_query as $single_post) {

        $title = get_the_title($single_post->ID);
        $taxonomy = 'clubes_dd';
        $exist = term_exists($title, $taxonomy);

            if ( $exist == FALSE )
                {
                wp_insert_term($title, 'clubes_dd');

        }
}
}
add_action('init','add_new_term_clubes');

1 个答案:

答案 0 :(得分:0)

function add_new_term_clubes() {
    $args = array(
        'posts_per_page'    =>  -1,
        'post_type'         =>  'clubes'
    );
    $title_query = get_posts('numberposts=-1&post_type=clubes');

    foreach ($title_query as $single_post) {

        $title = get_the_title($single_post->ID);
        $taxonomy = 'clubes_dd';
        $exist = term_exists($title, $taxonomy);

            if ( $exist == FALSE )
                {
                wp_insert_term($title, 'clubes_dd');
                wp_set_post_terms( $single_post->ID, $title, $taxonomy, true )

        }
}
}