simple question, how to get all tags of a custom post type by id? post_type=product.

i have tried with http://codex.wordpress.org/Function_Reference/wp_get_post_tags in my post loop and the print_r returning me nothing.

hence i tried this,

$term_list = wp_get_post_terms($post->ID, 'product_tag', array("fields" => "all"));

and it's getting me tags in my print_r($term_list); Thanks

  • @PieterGoosen i have tried the WordPress default tag function but need something customized, so just want solution, here now you can find what i did in my research.
    – sohan
    Mar 18, 2015 at 9:53
  • First you say "the print_r returning me nothing." then after you codeblock you say "it's getting me tags", is it returning nothing or is it returning the wrong tags? You're usnig the function correctly as far as I can tell so your next step would be to ensure that $post->ID is the ID you are expecting. The next step is to ensure that the $post->ID has tags in assigned to it in the admin panel. Finally, I would double check your taxonomy product_tag to ensure that it is spelled correctly.
    – Howdy_McGee
    Mar 18, 2015 at 13:50
  • wp_get_post_tags not giving me data while wp_get_post_terms giving me array data. by parsing it i can get tags. here tags are also custom type "product_tag" for post_type=product.
    – sohan
    Mar 19, 2015 at 10:18

4 Answers 4


Loop approach: usually archive-{custom_post}.php file.


custom_post_plural Stands for a group of custom posts of certain type.

Example of custom_post_plural: products

custom_post_singular Stands for an individual custom post type.

Example of custom_post_singular: product


var $args_custom_post_plural are the parameters of the WP_Query.

var $custom_post_plural is the execution of the query.

I used var $custom_post_plural_output to iterate the content of the WP_Object, specifically with the posts term, making the content of it "array friendly".

As you can see I partially used Ahmad instructions for a nested iteration.

   'post_type' => 'custom_post_singular',
   'post_status' => 'publish', 
   'posts_per_page' => -1, 
   'fields' => 'ids', 
   'order_by' =>'id', 
   'order' => 'ASC'
$custom_post_plural = new WP_Query($args_custom_post_plural);
$custom_post_plural_output = $custom_post_plural->posts;
for ($i=0; $i < count($custom_post_plural_output); $i++) { 
   $tags = wp_get_post_tags($custom_post_plural_output[$i]);
   $buffer_tags ='';
   foreach ( $tags as $tag ) {
      $buffer_tags .= $tag->name . ',';
echo $buffer_tags;


FYI If you want to use this in a single-{custom_post}.php file, you can use the following code:

$tags = wp_get_post_tags($post->ID);
foreach ( $tags as $tag ) {
   $buffer_tags .= $tag->name . ',';
echo $buffer_tags;

Since you must have a linked post in order to display anything.

Happy coding.

PS. @cjbj Why in the hell did you erase my edit, it has something wrong or what? Awful management here, and very malicious since I can't respond to a comment due to my reputation points amount.


If you need to get tags by post id you could use the followng function. This will work on anywhere since the method is based on database querying.

function sc_tf_get_tags_as_array($post_id){
        global $wpdb;
        $tbl_terms = $wpdb->prefix . "terms";
        $tbl_term_relationships = $wpdb->prefix . "term_relationships";

        $sql = "SELECT name FROM $tbl_terms WHERE term_id in (SELECT term_taxonomy_id FROM $tbl_term_relationships WHERE object_id='$post_id');";
        $results = $wpdb->get_results($sql);

            foreach($results as $row){
                $tags_list[] = $row->name;

        return $tags_list;

wp_get_post_tags works only for posts, not other post types. If you have a look to /wp-includes/post.php you will see it is calling wp_get_post_terms function with $taxonomy set to 'post_tag':

function wp_get_post_tags( $post_id = 0, $args = array() ) {
    return wp_get_post_terms( $post_id, 'post_tag', $args );

For product tags or other taxonomy you could use get_the_terms() instead :

$tags = get_the_terms( $prod_id, 'product_tag' );
$tags_names = array();
if ( ! empty( $tags ) ) {
    foreach ( $tags as $tag ) {
        $tags_names[] = $tag->name;
global $post;
$tags = wp_get_post_tags( $post->ID );
if ( $tags ) {
    foreach ( $tags as $tag ) {
        $tag_arr .= $tag->slug . ',';
    $args = array(
        'tag'           => $tag_arr,
        'post_per_page' => 10,
        'post__not_in'  => array( $post->ID ),
        'post_type'     =>'post_type_name'
    $related_posts = get_posts( $args );
    if ( $related_posts ) {
        foreach ( $related_posts as $post ) : setup_postdata( $post ); ?>
            <div class="swiper-slide">
                <a class="hover-effect image-holder" href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail(); ?>
                    <i class="icon-circle icon-circle--thin fa fa-arrow-right">&shy;</i>
        <?php endforeach;
  • 1
    Answers should be more than a code snippet. Please file an edit and explain why this would work.
    – Gabriel
    Nov 3, 2015 at 21:33
  • If you are the "Luis" who just tried to edit this question, please merge your accounts and re-edit it.
    – cjbj
    Aug 16, 2016 at 7:49

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.