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
4 Answers
Reset to default 1Loop 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">­</i>
</a>
</div>
<?php endforeach;
}
}
wp_reset_postdata();
$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 taxonomyproduct_tag
to ensure that it is spelled correctly. – Howdy_McGee ♦ Commented Mar 18, 2015 at 13:50