最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get all tags of a custom post type by id

programmeradmin3浏览0评论

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

i have tried with 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"));
print_r($term_list);

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

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

i have tried with http://codex.wordpress/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"));
print_r($term_list);

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

Share Improve this question edited Mar 18, 2015 at 9:49 sohan asked Mar 18, 2015 at 8:36 sohansohan 4071 gold badge4 silver badges11 bronze badges 3
  • @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 Commented 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 Commented 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 Commented Mar 19, 2015 at 10:18
Add a comment  | 

4 Answers 4

Reset to default 1

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

FIRST:

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

SECOND:

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.

$args_custom_post_plural=array(
   '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;

FINALLY:

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

        if($results){
            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>
                </a>
            </div>
        <?php endforeach;
    }
} 
wp_reset_postdata();
发布评论

评论列表(0)

  1. 暂无评论