如何根据Vimeo的点赞数量对帖子进行排序

时间:2012-01-18 作者:Bezbeli

我有许多帖子包含来自Vimeo的嵌入视频,我可以获得Vimeo的赞,并使用Vimeo API在帖子中显示它们,但我如何按Vimeo赞的数量排序这些帖子?

1 个回复
SO网友:mor7ifer

Couple of methods, depending on how much load you want to put on your server and how immediately the updates are required.

  1. Get all the posts, loop through them, adding a param to each post for the number of vimeo likes, then do a sort based on that.
  2. Store the number of vimeo likes as post meta, update this meta with cron, use WP_Query to sort by that meta field
  3. Potentially - Listen for the like change from Vimeo (not sure if possible) and, on recept, update the meta value for each post, same query method as number two.

If you pick one of these that suits your project best and need example code for it, post a comment, I can write it for any of them.

edit >> code for solution 2

add_action( \'wp_insert_post\', \'my_set_vimeo_meta\' );
function my_set_vimeo_meta( $the_id, $post ) {
    //$the_id contains the post id
    //$post contains the post object
    
    $vimeo_likes = ;//get your vimeo likes here, $post is your post object for any data you need
    
    update_post_meta( $the_id, \'vimeo_likes\', $vimeo_likes );
}

//run this function in cron, either WP or linux.
function my_vimeo_cron() {
    $args = array(
        \'post_status\' => \'publish\',
        \'post_type\'   => \'your_post_type\'
        //any additional filtering you want to do here to only get the posts that need updating.
    );
    $posts = WP_Query( $args );
    foreach( $posts as $k => $v ) {
        $vimeo_likes = ; // get your vimeo likes again, $v is your post object for any data you need
        
        update_post_meta( $v->ID, \'vimeo_likes\', $vimeo_likes );
    }
}

you can query that with WP_Query. It should update all your current posts as well the first time the cron runs. You probably want to tune this to your needs a good bit more, but it\'s most of the legwork done. Let me know if you need specifics on how to query the posts once they have the meta.

结束

相关推荐

Sort admin menu items

关于“的相关注释”Changing the Order of Admin Menu Sections?“,我正在寻找一种按字母顺序对WordPress管理区域的每个子部分中的条目进行排序的方法。目前,每当添加新插件时,其条目都会出现在“设置/工具/插件”下看似随机的位置,通常很难找到新的菜单项。(我已经有很多插件了,所以我的菜单很满。)由于我相当经常地添加和删除插件,所以我宁愿不需要不断进入设置页面来获取菜单排序插件并调整顺序。抱歉问了这么长的问题;我只是想弄清楚我在找什么。示例代替: S