Take the 2-minute tour ×

I have number of posts that contain embeded video from Vimeo, I can get vimeo likes and display them inside the post using vimeo API but how can I order those posts by number of Vimeo likes?

share|improve this question

1 Answer 1

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.

share|improve this answer
Thank you very much, 2. option sounds the best. –  Bezbeli Jan 18 '12 at 19:38
Added solution, it definitely needs tuning, but you should get the idea. –  m0r7if3r Jan 18 '12 at 19:57
can you be more specific please, im not so good with php. I've put my_vimeo_cron() function in cron.php and my_set_vimeo_meta() function in functions.php is this the right way? –  Bezbeli Jan 18 '12 at 23:29
also, to get vimeo links and display them inside post I use function getVimeoInfo($id, $info = 'stats_number_of_likes') { if (!function_exists('curl_init')) die('CURL is not installed!'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://vimeo.com/api/v2/video/$id.php"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $output = unserialize(curl_exec($ch)); $output = $output[0][$info]; curl_close($ch); return $output; } –  Bezbeli Jan 18 '12 at 23:52
No, you shouldn't ever modify core files if you can avoid it. Here's a good link on wp_cron, read over that, it should answer most of your questions. You need to set $vimeo_likes = getVimeoInfo( $video_id );...or at least assuming that the API responds with nothing more than a number, I've never worked with Vimeo's API specifically. I find the best way to approach these things is understand the script, which part is not clear to you? –  m0r7if3r Jan 19 '12 at 10:33

Your Answer


