• Hallo, i’m tring to get the list of terms/categories related to a specific custom post but i have odd results. The list of terms sometime include all the real terms related to a specific post, sometime the list doesn’t include all the terms.

    This is the code, i’m using this in get_footer hook:

    //getting all the taxonomies related to this specific post
    $taxonomies = get_object_taxonomies( $current_post_type );
    foreach ($taxonomies as $taxonomy) {
        //for each taxonomy i get the terms related to this post
        $terms = get_the_terms( get_the_ID(), $taxonomy );
        if( $terms != false ){
            foreach ($terms as $term) {
                //collecting the term name
                $result[] = $term->term_name;
            }
        }
    }
    

    This code seems to work but the reality is that i don’t get always all the terms.
    Which method are you using to complete the same task?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter danycode

    (@danycode)

    Can someone tell me if there are alternative ways to get the list of the terms related to a specific custom post? Thanks.

    Moderator bcworkz

    (@bcworkz)

    get_the_ID() must be called from inside the “loop”. Whatever function that gets the value for $current_post_type probably has a similar requirement. The ‘get_footer’ hook is not inside the loop, so the values returned by such functions is erratic, as you have discovered.

    If you only need this for a specific post and not the current post, pass specific values related to that post to the functions you use instead of passing values related to the current post, whatever that might be.

    To get terms for a specific post in a particular taxonomy, use wp_get_post_terms().

    Thread Starter danycode

    (@danycode)

    Thank you bcworkz, i fixed my script in this way, and now seems to work.

    The value from get_the_ID was definitely wrong, so i replaced it with get_queried_object_id(). With the correct post id now get_post_type() should work properly.

    I’ve also replaced get_the_terms() with wp_get_post_terms() as you suggested.

    
    //i found this method to get the current post id outside the loop
    $current_post_id = get_queried_object_id();
    $current_post_type = get_post_type( $current_post_id );
    
    //get an array with the taxonomies of this post type
    $taxonomies = get_object_taxonomies( $current_post_type );
    
    //loop over the available taxonomies of this post
    foreach ($taxonomies as $taxonomy) {
    
        //get all the terms associated with the current post for this $taxonomy
        $terms = wp_get_post_terms( $current_post_id, $taxonomy );
        if( $terms != false ){
            foreach ($terms as $term) {
                 $result[] = $term->term_name;
            }
    
        }
    
    }
    

    Does it look good in your opinion? And do you know the real difference between this functions: get_the_terms, wp_get_post_terms, wp_get_object_terms

    Moderator bcworkz

    (@bcworkz)

    Looks good to me, especially if it works ??

    The differences are pretty much as the names imply. wp_get_post_terms() only gets terms for posts. wp_get_object_terms() gets terms for any object, not just posts. get_the_terms() is a generic low level function that gets whatever terms match the arguments you feed it with no restrictions. With more specific functions, you can get results with fewer arguments, with get_the_terms(), you must specify everything.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get the list of the terms/categories related to a specific custom post’ is closed to new replies.