当一个元值达到一个设置的数字时,是否可以向帖子添加自定义分类法?
我想得到它所以一旦我post_views_count
达到150以上,然后获得自定义分类法trending
添加到它。
我发现做一些类似于我想要实现的事情的唯一方法是add_action( \'save_post\' );
钩据我所知,这只适用于后期保存。这是无用的post_views_count
每次更新update_post_meta
已更新。
function getPostViews($postID){
$count_key = \'post_views_count\';
$count = get_post_meta($postID, $count_key, true);
if($count==\'\'){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, \'0\');
return "0 View";
}
return $count.\' Views\';
}
function setPostViews($postID) {
$count_key = \'post_views_count\';
$count = get_post_meta($postID, $count_key, true);
if($count==\'\'){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, \'0\');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
function ViewPostsTrending( $classes ) {
global $post;
$count_key = \'post_views_count\';
$count = get_post_meta($post->ID, $count_key, true);
if ( $count > 150 ) {
$classes[] = \'trending\';
}
return $classes;
}
add_filter( \'post_class\', \'ViewPostsTrending\');`