如何在WordPress中为自定义帖子类型添加自定义术语?

时间:2017-12-05 23:00:53

标签: php wordpress

我有一个名为“服务”的自定义帖子类型。我在前端有一个表单,用户可以使用它创建服务。服务还可以使用5种分类法。当用户创建服务时,我希望他们能够为他们的服务添加标签,无论他们为他们的服务选择哪种分类。我的脚本目前通过创建帖子开始,然后检查每个标签是否存在,如果标签不存在,则插入,如果存在,则使用现有标签的id。我不断收到WP错误“无效的分类标准”。

有没有办法将术语添加到自定义帖子类型而不管它的分类?

以下是注册CPT和分类法的代码:

function init(){
    register_post_type('service', 
            array(
                'labels' => array(
                        'name' => 'Services',
                        'singlular_name' => 'Service',
                        'add_new' => 'Add New',
                        'add_new_item' => 'Add New Service',
                        'edit_item' => 'Edit Service',
                        'new_item' => 'New Service',
                        'view_item' => 'View Service',
                        'view_items' => 'View Services',
                        'search_items' => 'Search Services',
                        'not_found' => 'No services found',
                        'not_found_in_trash' => 'No services found in trash',
                        'all_items' => 'All Services',
                        'archives' => 'Archived Services',
                        'featured_image' => 'Service Logo',
                        'use_featured_image' => 'Use as logo',
                        'remove_featured_image' => 'Remove logo',
                        'set_featured_image' => 'Set logo',
                        'menu_name' => 'Services'
                ),
                'description' => 'Services for events provided by people.',
                'public' => true,
                'publicly_queryable' => true,
                'show_in_admin_bar' => true,
                'menu_icon' => 'dashicons-id-alt',
                'capability_type' => 'services',
                'hierarchical' => false,
                'supports' => array('title', 'author', 'thumbnail', 'custom-fields', 'comments', 'revisions'),
                'has_archive' => true           
            )
        );

    $band_labels = array(
            'name' => _x( 'Bands', 'taxonomy general name' ),
            'singular_name' => _x( 'Band', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search Bands' ),
            'all_items' => __( 'All Bands' ),
            'parent_item' => __( 'Parent Band' ),
            'parent_item_colon' => __( 'Parent Band:' ),
            'edit_item' => __( 'Edit Band' ),
            'update_item' => __( 'Update Band' ),
            'add_new_item' => __( 'Add New Band' ),
            'new_item_name' => __( 'New Band Name' ),
            'menu_name' => __( 'Bands' ),
        );

        // Now register the taxonomy

        register_taxonomy('Band',array('service'), array(
        'hierarchical' => false,
        'labels' => $band_labels,
        'show_ui' => true,
        'show_admin_column' => false,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'bands' ),
        ));
}
add_action('init', 'init);

这是我修改服务的地方:

function modify(){
        $post = get_post($this->id);
        if($post != null){
            $post_id = wp_update_post(array(
                'ID' => $this->id,
                'post_title' => $_POST['business_name']
            ));
            if($_POST['location'] != $this->info['meta']['service_location'][0]){
                update_post_meta($this->id, 'service_location', $_POST['location']);
                $this->geocode($_POST['location']);
                update_post_meta($this->id, 'service_lat', $this->geocode['lat']);
                update_post_meta($this->id, 'service_lon', $this->geocode['lon']);
            }
            $meta = array('description', 'max_distance', 'category', 'website', 'email');
            foreach($meta as $key){
                if($_POST[$key] != $this->info['meta']['service_'.$key][0] || !isset($this->info['meta']['service_'.$key][0])){
                    if(get_post_meta($this->id, 'service_'.$key, true)){
                        update_post_meta($this->id, 'service_'.$key, $_POST[$key]);
                    } else {
                        add_post_meta($this->id, 'service_'.$key, $_POST[$key]);
                    }
                }
            }
            $tags = explode(',', $_POST['tags']);
            $keywords = array();
            if(count($tags) > 0){
                foreach($tags as $tag){
                    $term_exists = term_exists($tag);
                    if($term_exists !== 0 && $term_exists !== null){
                        $term = $term_exists;
                    } else {
                        $new_term = wp_insert_term($tag, 'service');
                        if(!is_wp_error($new_term)){
                            $term = $new_term['term_id'];
                        } else {
                            die($new_term->get_error_message());
                        }
                    }
                    $keywords[] = $term;
                }
            }
            $keywords = array_map('intval', $keywords);
            $keywords = array_unique($keywords);
            $add_terms = wp_set_object_terms($this->id, $keywords, 'service');
            return true;
        } else {
            return false;
        }
    }

0 个答案:

没有答案