0

function get_post_primary_category($post_id, $term='category', $return_all_categories=false){
    $return = array();
     if (class_exists('WPSEO_Primary_Term')){
        // Show Primary category by Yoast if it is enabled & set
        $wpseo_primary_term = new WPSEO_Primary_Term( $term, $post_id );
        $primary_term = get_term($wpseo_primary_term->get_primary_term());
         if (!is_wp_error($primary_term)){
            $return['primary_category'] = $primary_term;
        }
    }
     if (empty($return['primary_category']) || $return_all_categories){
        $categories_list = get_the_terms($post_id, $term);
         if (empty($return['primary_category']) && !empty($categories_list)){
            $return['primary_category'] = $categories_list[0];  //get the first category
        }
        if ($return_all_categories){
            $return['all_categories'] = array();
             if (!empty($categories_list)){
                foreach($categories_list as &$category){
                    $return['all_categories'][] = $category->term_id;
                }
            }
        }
    }
     return $return;
}

HOW TO USE THIS FUNCTION:

$post_categories = get_post_primary_category($post->ID, 'product_cat'); 
$primary_category = $post_categories['primary_category'];

Jagdish Sarma Edited question August 7, 2020
Add a Comment