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

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

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论