I’m building a plugin that exports posts to a CSV. The thing is, I’m using the stats\_get\_csv() function from Jetpack to get the views for each post, and it’s slowing down my download speed to 100 Bytes/s. It’s a disaster.
What I’ve already tried and didn’t work:
1. I tried doing a direct SQL query to get the post views instead of calling stats\_get\_csv() for each one. But it didn’t work because it fetches different views, not the same ones.
2. I thought about using transients to cache the results of stats\_get\_csv() and avoid repeated calls during the export. But nope, it didn’t improve things.
3. I attempted to fetch all the data at once to avoid multiple requests. But I get a timeout before I can export. It’s a bummer.
Honestly, I don’t know what else to try. I feel like the meme of the guy looking at the piece of paper with a suffering face.
If anyone has any ideas or experience with this function and can lend me a hand, I’ll be eternally grateful. I need to solve this, otherwise my plugin is going to be slow as hell.
Thanks in advance!
My function to get views:
function get\_page\_views($post\_id) {
$cache\_key = ‘post\_views\_’ . $post\_id;
$views = wp\_cache\_get($cache\_key);
if ($views === false) {
if (function\_exists(‘stats\_get\_csv’)) {
$args = array(‘days’=>-1, ‘limit’=>-1, ‘post\_id’=>$post\_id);
$result = stats\_get\_csv(‘postviews’, $args);
$views = intval($result\[0\]\[‘views’\]);
} else {
$views = 0;
}
wp\_cache\_set($cache\_key, $views, ‘post\_views’, 300);
}
return $views;
}
part of my export function:
foreach ($results as $post) {
$views = wp\_cache\_get(‘post\_views\_’ . $post->ID);
if ($views === false) {
$views = get\_page\_views($post->ID);
error\_log(‘Visitas obtenidas de la forma original para el post\_id: ‘ . $post->ID . ‘, visitas: ‘ . $views);
} else {
error\_log(‘Visitas obtenidas de la caché para el post\_id: ‘ . $post->ID . ‘, visitas: ‘ . $views);
}
fputcsv($output, array(
$post->ID,
$post->post\_title,
get\_the\_author\_meta(‘display\_name’, $post->post\_author),
implode(‘, ‘, wp\_get\_post\_categories($post->ID, \[‘fields’ => ‘names’\])),
$views,
$post->post\_date
));
ob\_flush();
flush();
}
[ad_2]
`stats_get_csv()` relies on [the legacy WordPress.com Stats API](https://stats.wordpress.com/csv.php). It works, but it has less flexibility than if you were to use the newer (introduced 10 years ago) [WordPress.com REST API endpoint](https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/stats/post/%24post_id/).
The Jetpack plugin comes with a few wrappers to help you authenticate your REST API requests to WordPress.com. That’s where I would look first. `( new AutomatticJetpackStatsWPCOM_Stats() )->get_post_views( $post_id )` is probably a good place to start ([reference](https://github.com/Automattic/jetpack/blob/5c59a37f3d311bee32271f79622c2527f5afb6bb/projects/packages/stats/src/class-wpcom-stats.php#L202-L211)).
Once you have that figured out, I would recommend checking if you can make that request asynchronously, right before the export, instead of stopping your export to make that request for each post ID.
I should also mention that if you have other questions like this, feel free to post in the Jetpack support forums on WordPress.org.