How to optimize Jetpack Page Visits Counter?
-
I am using
do_shortcode( '[jp_post_view]' );
to show Jetpack visits count in mycontent.php
file as shown in the below snippet:if ( 'post' === get_post_type() ) { twentyfourteen_posted_on(); ?> <span class="comments-link"><?php echo do_shortcode( '[jp_post_view]' );?></span> <?php }
This is working fine in single posts, but it is very slow in archive pages (search, tag, and category pages). The final result is as shown below:
The problem is my archive pages are taking 20-25 seconds to load, which is not acceptable for visitors at all. I used to display 20 posts in archive pages, but I changed this back to the default value (10) which reduced the loading time to 7-10 seconds. Still, this is not within the acceptable range.
Using Query Monitor, I found out that I have many duplicated queries because of this visit counter (40 queries in this case).
If I put the counter in
single.php
instead ofcontent.php
, the loading issue will be resolved. However, I would like to show this counter in my archive page as well but without affecting the performance.I am a beginner in PHP and WP world, so I am not sure how to efficiently reduce the number of DB round trips in such a case. How can I get the “visit counter” field with the remaining post meta data in one query to avoid calling the DB again and again per post? Any suggestions?
- The topic ‘How to optimize Jetpack Page Visits Counter?’ is closed to new replies.