Count tags for current post and save into custom meta field (and update it on post edit)

This question is sort of a follow up from another question on here Order posts by tags count? The first comment recommen

This question is sort of a follow up from another question on here Order posts by tags count?

The first comment recommends counting the number of tags and saving it into a custom meta field. Going from that, I've put this in my single.php

$terms = get_the_terms( $post->ID, 'taxonomy' );
$terms_count = count ( $terms );
if ( ! add_post_meta($post->ID, 'custom-field-name', $terms_count, true ) ) { 
        update_post_meta( $post->ID, 'custom-field-name', $terms_count );
}

This first question is - is this code alright? It seems to work fine, but I basically just copied it over from the Wordpress codex.

The second question - is there a way to trigger this function when I update the post, instead of it being updated every time the page is reloaded? Seems redundant this way.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745310666a4621982.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信