2

There is a new feature in wordpress introducing primary category. When you select a category, primary category can be specified.

My question is how can I get that primary category name using Wordpress Core functions?

if there is no function, can you help me to get the first child of main category?

for example:
- main category
-- child cat 1
-- child cat 2
-- child cat 3

I need to get -- child cat 1.

Thanks for you help.

  • Primary category ? Can you please help ME to find a article and this new concept primary category ? – Sumit Jun 5 '16 at 8:23
  • Install the latest version of wordpress then add new post select multiple categories one of the categories can be selected as primary category – MMT designer Jun 5 '16 at 9:00
  • Or maybe I am mistaking and a plugin giving me this options?! – MMT designer Jun 5 '16 at 9:01
  • 2
    I don't see this. Are you taking about this yoast.com/yoast-seo-3-1 ? – Sumit Jun 5 '16 at 9:15
  • 2
    @MMTdesigner It's Yoast SEO that is providing the 'primary category' feature. They are using it for breadcrumb. It's not from WordPress core. :) – Mayeenul Islam Jun 5 '16 at 9:18
2

I have editted the MMT Designer function. This works better for me:

if ( ! function_exists( 'get_primary_taxonomy_id' ) ) {
    function get_primary_taxonomy_id( $post_id, $taxonomy ) {
        $prm_term = '';
        if (class_exists('WPSEO_Primary_Term')) {
            $wpseo_primary_term = new WPSEO_Primary_Term( $taxonomy, $post_id );
            $prm_term = $wpseo_primary_term->get_primary_term();
        }
        if ( !is_object($wpseo_primary_term) || empty( $prm_term ) ) {
            $term = wp_get_post_terms( $post_id, $taxonomy );
            if (isset( $term ) && !empty( $term ) ) {
                return $term[0]->term_id;
            } else {
                return '';
            }
        }
        return $wpseo_primary_term->get_primary_term();
    }
}
1

To answer your second question: get_categories() allows you to pass a whole bunch of arguments, one of which happens to be child categories.

First get the parent category. I've used get_category_by_slug here, but you could use any other way to get it, for instance Yoasts function to retrieve the primary category.

$category = get_category_by_slug( 'category-name' );

Then get all child categories:

$args = array(
'type'                     => 'post',
'child_of'                 => $category->term_id,
'orderby'                  => 'name',
'order'                    => 'ASC', // or any order you like
'hide_empty'               => FALSE,
'hierarchical'             => 1,
'taxonomy'                 => 'category',
); 
$child_categories = get_categories($args );

Finally select the first element if there is any:

if !empty($child_categories) $first_child = $child_categories[0];
  • Tanks this is nice way to do it, I have found other ways but I guess your way is more efficient thank you so much. – MMT designer Jun 6 '16 at 4:42
1

I see that this question is getting a lot of attentions since the past year, I thought to answer this question in the right way.

There is not primary category in wordpress if you have installed Yoast SEO plugin then a new feature will be appear on Single Posts category selection in admin area in order to choose primary category .

To get that primary category you can use the following function I came up with:

if ( ! function_exists( 'get_primary_taxonomy_id' ) ) {
function get_primary_taxonomy_id( $post_id, $taxonomy ) {
    $prm_term = '';
    if (class_exists('WPSEO_Primary_Term')) {
        $wpseo_primary_term = new WPSEO_Primary_Term( $taxonomy, $post_id );
        $prm_term = $wpseo_primary_term->get_primary_term();
    }
    if ( !is_object($wpseo_primary_term) && empty( $prm_term ) ) {
        $term = wp_get_post_terms( $post_id, $taxonomy );
        if (isset( $term ) && !empty( $term ) ) {
            return wp_get_post_terms( $post_id, $taxonomy )[0]->term_id;
        } else {
            return '';
        }
    }
    return $wpseo_primary_term->get_primary_term();
}
}

First it will check to see if Yoast SEO is being installed and activated then it will try to get primary category. if Yost is not installed then it will get all of the categories and returns the first one.

Notice how this function also works for custom post types with custom taxonomies.

At the end this function returns the category (term) ID if you want to get the category (term) object you can use get_term($ID, $taxonomy) and pass in the ID

1

this is not for wordpress but for seo plugin, you could use following function

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 

    }
    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;
}

this function is written by lab21.gr

0

A native Wordpress solution to return the current parent category.

function primary_categories($arr_excluded_cats) {

if($arr_excluded_cats == null) {
    $arr_excluded_cats = array();
}

$post_cats = get_the_category();

$args = array(
  'orderby' => 'name',
  'order' => 'ASC',
  'parent' => 0
);

    $primary_categories = get_categories($args);

    foreach ($primary_categories as $primary_category) {

        foreach ($post_cats as $post_cat) {
            if(($primary_category->slug == $post_cat->slug) && (!in_array($primary_category->slug, $arr_excluded_cats))) {
                return $primary_category->slug;
            }
        }
    }
}

//if you have more than two parent categories associated with the post, you can delete the ones you don't want here
$dont_return_these = array(
        'receitas','enciclopedico'
    );

//use the function like this:
echo primary_categories($dont_return_these);

Comments:

  • if you only have one parent category by post, pass null instead of array
  • if you want another output instead of slug change this to return $primary_category-> slug;
-2

I use sCategory Permalink plugin https://wordpress.org/plugins/scategory-permalink/

$category_primary_id = get_post_meta(get_the_ID(), '_category_permalink', true);
if (!empty($category_primary_id)) {
    $category = get_category($category_primary_id);
    var_dump($category->name);
}
  • What is _category_permalink ? I'm not familiar with that metakey. Where do I put this code? Could you edit your answer using the edit link above and add in some detail to your answer, please? – Howdy_McGee Mar 31 '17 at 14:26
  • > What is category_permalink ? Each primary category for post saved as _category_permalink – Serge Andreev Apr 4 '17 at 8:26
  • > I'm not familiar with that metakey. Use SQL query for find all metakeys for post: SELECT * FROM postmeta WHERE post_id = ID – Serge Andreev Apr 4 '17 at 8:27
  • > Where do I put this code? For example, you can insert this code to wp-content/themes/<your-theme>/single.php or some different PHP file, where you show Primary category name for post – Serge Andreev Apr 4 '17 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.