/** * Cron to process all of the posts that don't have bitly urls */ function bitly_process_posts() { global $wpdb; // get 100 published posts that don't have a bitly url $query = "\n\t\tSELECT {$wpdb->posts}.ID\n\t\tFROM {$wpdb->posts}\n\t\tWHERE NOT EXISTS (\n\t\t\tSELECT ID\n\t\t\tFROM {$wpdb->postmeta}\n\t\t\tWHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id\n\t\t\tAND {$wpdb->postmeta}.meta_key = 'bitly_url'\n\t\t)\n\t\tAND ( {$wpdb->posts}.post_type = 'post' OR {$wpdb->posts}.post_type = 'page' )\n\t\tAND ( {$wpdb->posts}.post_status = 'publish' )\n\t\tGROUP BY {$wpdb->posts}.ID\n\t\tORDER BY {$wpdb->posts}.post_date DESC\n\t\tLIMIT 0, 100\n\t\t"; $posts = $wpdb->get_results($query); if ($posts) { // process these posts foreach ($posts as $p) { Bitly::generate_bitly_url($p->ID); } } else { // kill our scheduled event add_option('bitly_processed', 1); wp_clear_scheduled_hook('bitly_hourly_hook'); } }