WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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. – mor7ifer 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? – mor7ifer Jan 19 '12 at 10:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.