由于不知道您的输入表单是什么样子,我只能给您以下建议。基本上,您需要验证您是否想要/拥有保存数据的权限,然后接受元的输入,并通过wp_set_object_terms()
. 您将希望在每次保存帖子时激发此。。。ie在save_post
钩
function save_taxonomy_data($post_id) {
// verify this came from our screen and with proper authorization.
// your form should have a nonce <?php wp_nonce_field(\'taxonomy_country\',\'taxonomy_noncename\'); ?>
if ( !wp_verify_nonce( $_POST[\'taxonomy_noncename\'], \'taxonomy_country\' )) {
return $post_id;
}
// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return $post_id;
// Check permissions
if ( \'page\' == $_POST[\'post_type\'] ) {
if ( !current_user_can( \'edit_page\', $post_id ) )
return $post_id;
} else {
if ( !current_user_can( \'edit_post\', $post_id ) )
return $post_id;
}
// OK, we\'re authenticated: we need to find and save the data
// right now it is only working on posts and pages, need to adapt to other types here
$post = get_post($post_id);
if ( ($post->post_type == \'post\') || ($post->post_type == \'page\') ) {
isset( $_POST[\'post_country\'] )
wp_set_object_terms( $post_id, $_POST[\'post_country\'], \'country\' );
}
}
add_action(\'save_post\', \'save_taxonomy_data\');
改编自
Custom Taxonomy Panels 也类似于
Unable to save custom taxonomy terms in a custom-built metabox.
Second Attempt
第一次尝试主要是如何从元数据库保存分类数据。我浏览了一下WP用户前端代码,并做了一些调整。这是为了替换上面的内容,并假设cf\\u country是输入的name元素。
function save_taxonomy_data( $post_id ) {
// verify this came from our screen and with proper authorization.
if ( ! wp_verify_nonce( $_POST[\'_wpnonce\'], \'wpuf-add-post\' ) )
return;
// OK, we\'re authenticated: we need to find and save the data
if ( isset( $_POST[\'cf_country\'] ) ) {
wp_set_object_terms( $post_id, $_POST[\'cf_country\'], \'country\' );
}
}
add_action(\'wpuf_add_post_after_insert\', \'save_taxonomy_data\');
Third Attempt:
function save_taxonomy_data($post_id) {
/*
* verify this came from our screen and with proper authorization.
* your form should have a nonce <?php wp_nonce_field(\'wpuf-add-post\',\'_wpnonce\'); ?>
*/
if ( !wp_verify_nonce( $_POST[\'_wpnonce\'], \'wpuf-add-post\' )) {
return $post_id;
}
// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return $post_id;
// Check permissions
if ( \'page\' == $_POST[\'wpuf_post_type\'] ) { // post type is a hidden field in wpuf_
if ( !current_user_can( \'edit_page\', $post_id ) )
return $post_id;
} else {
if ( !current_user_can( \'edit_post\', $post_id ) )
return $post_id;
}
// Work on all post types
if ( isset( $_POST[\'post_country\'] ) )
wp_set_object_terms( $post_id, $_POST[\'post_country\'], \'country\' );
}
add_action(\'save_post\', \'save_taxonomy_data\');