WordPress.org

Ready to get started?Download WordPress

Forums

Get ID of current taxonomy (2 posts)

  1. Raisis
    Member
    Posted 1 year ago #

    Hi everbody,

    I like to output the ID of the current taxonomy ID I'm using. I'm looking for something like <?php the_category_ID(); ?>

    While searching via Google I found code examples like this:

    <?php $terms = get_the_terms( $post->ID , 'taxonomy-name' );
    if($terms) {
    	foreach( $terms as $term ) {
    		echo $term->term_id.'<br />';
    	}
    }
    ?>

    The problem is that I can't set the 'taxonomy-name', because it's an unordered list of posts with different taxonomies. So I need something, that WordPress uses automatically the taxonomy-name of the actual post.

    Can somebody helps me?

    Thank you!

    Greetings from Germany,
    Raisis

  2. haxxxton
    Member
    Posted 1 year ago #

    you could do a $wpdb query like

    //get the IDs
    $taxonomy_IDs = $wpdb->get_col("SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = '".get_the_ID()."'");
    //output the IDs
    echo implode(',', $taxonomy_IDs);

    but you'd be doing that for EACH post.. and is a little server heavy

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags