当元值达到或超过设定的数值时,自动添加自定义分类

时间:2015-07-23 作者:Thomas \'Renny\' Renshaw

当一个元值达到一个设置的数字时,是否可以向帖子添加自定义分类法?

我想得到它所以一旦我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\');`

1 个回复
SO网友:Pieter Goosen

我认为最好的选择是使用getPostViews() 计算并在其中合并一个系统,以附加trending 当你的计数器达到150时,立即向帖子发送条件。

这是我们的工作流程,请获取后期视图计数(,您已经在这样做了)

将当前计数与所需的150进行比较

使用wp_set_object_terms() 要附加trending 任职期限

代码中的

    我稍微修改了您的代码。另请注意,代码未经测试)

    function getPostViews()
    {
        // Check if we are on a single page, bail out if not
        if ( !is_single() )
            return false;
    
        // Get the current post id
        $current_post_id = get_queried_object_id();
    
        $count_key = \'post_views_count\';
        $count = get_post_meta( $current_post_id, $count_key, true );
    
        // Set up our function to set our term \'trending\'
        if ( $count >= 150 ) {
            /**
             * First check if the term is not yet set, if not, set the term
             * I have used trending as term slug. Change as needed
             * Also, add the correct taxonomy to which trending term belongs to
             *
             * @link https://codex.wordpress.org/Function_Reference/has_term
             */
            if ( !has_term( \'trending\', \'MY_TAX\', $current_post_id ) ) {
                /**
                 * Append the term trending to the post
                 *
                 * @link https://codex.wordpress.org/Function_Reference/wp_set_object_terms
                 */
                wp_set_object_terms( 
                    $current_post_id, // Current post ID
                   \'trending\', // Slug of term to append
                   \'MY_TAX\', // Taxonomy that the term trending belongs to
                   true // VERY VERY IMPORTANT, Append the term to the other existing terms
                );
            }
        }
    
        if( !$count ){
    
            delete_post_meta( $current_post_id, $count_key );
            add_post_meta( $current_post_id, $count_key, \'0\' );
    
        }
    
        $views = ( $count ) ? $count . \' Views\' : \'0 View\';
        return $views;
    
    }
    
    您只需在单个页面中使用以下代码

    echo getPostViews();
    
    剩下的由函数完成

结束

相关推荐

为什么Pre_Get_Posts挂钩会导致Apache停止工作?

我的所有帖子都有一个名为\'validity\' 我在那里指定日期,直到帖子生效。我的分类法档案显示了所有的帖子。现在我想在那里过滤,并排除无效的帖子。我写道:function e_exclude_posts( $query ) { if( $query->is_tax(\'my_tax\') ) { $query->set( \'post__not_in\', array( 530 ) ); } } add_action( \