以下是我最终的做法:
首先是选项页面(用户可以从该页面查看所有品牌并添加新品牌)。
加载时,新品牌字段设置为“”(空)。
// Reset new brand field
add_filter(\'acf/load_field/key=field_52d6f9449e688\', \'reset_new_brand\');
function reset_new_brand( $field )
{
// reset choices
$field[\'value\'] = \'\';
// Important: return the field
return $field;
}
保存选项页时,将创建一个新类别
// run AFTER ACF saves the $_POST[\'fields\'] data
add_action(\'acf/save_post\', \'my_acf_save_option_page\', 20);
function my_acf_save_option_page( $post_id ){
$brand_to_add = get_field_object(\'field_52d6f9449e688\', \'option\');
if( !empty($brand_to_add[\'value\'])){
wp_insert_term( $brand_to_add[\'value\'], \'marque\' );
//delete_option(\'marque_children\'); // clear the cache
}//end option page stuff
}
编辑帖子时,所有品牌都会从类别中加载
// Load Brand on edit product
add_filter(\'acf/load_field/key=field_52b2c7c6bc3e5\', \'load_marque_field\');
function load_marque_field( $field )
{
// reset choices
$field[\'choices\'] = array();
// get the textarea value from options page without any formatting
$choices = get_terms(\'marque\', array(\'parent\' => 0, \'hide_empty\' => false, \'orderby\'=> \'name\' ));
$arr = array();
// loop through array and add to field \'choices\'
foreach( $choices as $tax_term ) {
$arr[$tax_term->slug] = $tax_term->name;
}
$field[\'choices\'] = $arr;
// Important: return the field
return $field;
}
编辑或创建帖子时,会有一个名为“new model”的高级自定义字段,如果设置了此字段,则在保存/更新帖子时,会在所选品牌上创建一个新的子类别。保存帖子时,我还会检查是否有任何子类别的计数为0,它们将被删除。
// run BEFORE ACF saves the $_POST[\'fields\'] data
add_action(\'acf/save_post\', \'my_acf_save_post\', 1);
//Register terms from new model field and category form brand
function my_acf_save_post( $post_id ){
// vars
$fields = false;
if(get_post_type( $post_id ) === "pompitup_product"){
// load from post
if( isset($_POST[\'fields\']) ){
$fields = $_POST[\'fields\'];
$selected_brand = $fields[field_52b2c7c6bc3e5];
$new_tax_field = $fields[field_52c53c86e1fb2];
$existing_tax_field = $fields[field_52dbf2af72c54];
if( isset($new_tax_field)){
$new_tax = $new_tax_field;
update_field(\'field_52dbf2af72c54\', $new_tax, $post_id);
//check if the term exist in selected brand
$term = term_exists($new_tax, $selected_brand);
if ($term == 0 ) {
//get selected brand infos (id)
$getTermOptions = get_term_by(\'slug\', $selected_brand, \'marque\');
//insert new term in selected brand
wp_insert_term( $new_tax, \'marque\', array( \'parent\'=> (int)$getTermOptions->term_id ) );
delete_option(\'marque_children\'); // clear the cache
}
}else{
if( isset($existing_tax_field)){ $new_tax = $existing_tax_field; }
}
//set category (brand)
wp_set_object_terms( $post_id, $selected_brand, \'marque\', false );
//set category model
wp_set_object_terms( $post_id, $new_tax, \'marque\', true );
//delet emtpy tax
$taxonomyName = "marque";
//This gets top layer terms only. This is done by setting parent to 0.
$parent_terms = get_terms($taxonomyName, array(\'parent\' => 0, \'hide_empty\' => false));
foreach ($parent_terms as $pterm) {
//Get the Child terms
$terms = get_terms($taxonomyName, array(\'parent\' => $pterm->term_id, \'hide_empty\' => false));
foreach ($terms as $term) {
if( 0 == $term->count ){
wp_delete_term( $term->term_id, $term->taxonomy);
}
}
}
}//end isset($_POST[\'fields\'])---->for post
}//end check if post-type== pompitup_product
// ...
}//end my_acf_save_post