首页 > 解决方案 > 根据帖子元自动选择分类

问题描述

我使用 jetengine 名称listing为电子商务网站评论创建了一个自定义帖子类型,并且它有platform一个下拉选择框。

此帖子类型具有称为payment method&的分类法,shipping method其中包含多个术语

因此,当我创建新帖子时,如果我选择A 那么它会自动在andplatform中选择多个术语(复选框)吗?payment methodshipping method

提前致谢

标签: wordpresstaxonomytaxonomy-terms

解决方案


试试这个代码

您需要用您的数据填充 $data_array 并将分类 slug 更改为您的

add_action( 'save_post_listing', 'listing_save_payment_term' );
function listing_save_payment_term($post_id) {

       $data_array = array(
       "platform_name" => "payment method & shipping method terms_ids comma separated in array",
       "platform_name2" => array( 15,12,18),
       "platform_name3" => array( 19,18),
       "platform_name4" => array( 19,18),
       );
       
        remove_action( 'save_post_listing', 'listing_save_payment_term' );

        $meta = get_post_meta($post->ID, "platform", true);
        
        
        if($meta != "") {           
        
        $platform_term_ids = $data_array[$meta];
        if($platform_term_ids != "") {
            
        //change here for your payment method & shipping method taxonomy slug
        wp_set_object_terms($post_id, $platform_term_ids, 'payment method & shipping method slug');
        }
        clean_post_cache( $post_id );
        
        } else {
            
        //change here for your payment method & shipping method taxonomy slug
        wp_delete_object_term_relationships( $post_id, 'payment method & shipping method slug' );
        
        }
        
        
        add_action( 'save_post_listing', 'listing_save_payment_term' );
}

推荐阅读