Possible to assign popular posts to a tag or category?
-
Hi all
Am hoping the plugin helps to assign the popular posts to tag or category and remove if it falls below.
I tried reading the faq and searching here but no luck . .
Thank you!
-
Hi @confusedkoala,
Unfortunately no, the plugin has no such feature. It is doable though, only it needs a bit of coding (you’ll need to fetch the current list of popular posts, add/remove posts to/from your popular tag/category, then repeat after a certain amount of time has passed).
If you’re not familiar with coding in general I can help out with this but not today as I need some rest, it’s been a tiresome week haha.
That will be most kind of you. Thank you.
Hope you got your rest ??
Hope you got your rest ??
I did, thanks ??
Alright, untested but I believe this should work. Add this code snippet to your site (here’s how):
/** * Tags popular posts as "popular". * Updates every hour. */ function tag_popular_posts(){ if ( false === get_transient('tag_popular_posts') ) { // Check if we have popular posts to tag $args = [ 'range' => 'last7days', 'post_type' => 'post', 'limit' => 5 ]; $popular_posts = new \WordPressPopularPosts\Query($args); // Popular posts found if ( $popular_posts->get_posts() ) { // Get current posts tagged as popular $tagged_posts = get_posts( [ 'post_type' => 'post', 'posts_per_page' => -1, 'tax_query' => [ [ 'taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => 'popular', ], ], 'fields' => 'ids' ] ); // Remove "popular" tag from previously tagged posts if ( ! empty($tagged_posts) ) { foreach($tagged_posts as $tagged_post_id) { $tags = wp_get_object_terms($tagged_post_id, 'post_tag'); $new_tags = array(); foreach($tags as $tag) { if ( 'popular' != $tag->slug ) { $new_tags[] = $tag; } } wp_set_object_terms($tagged_post_id, $new_tags, 'post_tag', false); } } // Add "popular" tag to the current list of popular posts foreach($popular_posts->get_posts() as $popular_post) { $tags = wp_get_object_terms($popular_post->id, 'post_tag'); $tags[] = 'popular'; wp_set_object_terms($popular_post->id, $tags, 'post_tag', false); } // Don't update for another hour set_transient('tag_popular_posts', 1, 1 * HOUR_IN_SECONDS); } } } add_action('wpp_post_update_views', 'tag_popular_posts');
We’re not done yet though. You’ll also need to adjust this bit of the code so it matches the settings you’re using with your popular posts list:
// Check if we have popular posts to tag $args = [ 'range' => 'last7days', /* Valid values: all, last24hours, last7days, last30days*/ 'post_type' => 'post', /* Set your post type(s) here, comma separated */ 'limit' => 5 /* Change this too */ ];
(For a more detailed explanation of each argument please see Settings > WordPress Popular Posts > Parameters.)
Likewise, you’ll need to adjust one line here as well:
// Get current posts tagged as popular $tagged_posts = get_posts( [ 'post_type' => 'post', /* Change this to your desired post type(s) */ 'posts_per_page' => -1, 'tax_query' => [ [ 'taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => 'popular', ], ], 'fields' => 'ids' ] );
The code assumes that you’re using WordPress Popular Posts 5.0.0 or newer. If you aren’t, please update the plugin before attempting to use this code.
If you have any questions don’t hesitate to ask.
Thank you!
you are probably one of the best developers around ??
May i also check, my hosts is complaining about max account executions exceeding . . , my current setting is cache 1 min will changing it to 10 mins help? ( although i am not sure if its this plugin causing the heavy usage )
Thank you!
you are probably one of the best developers around ??
No problem, glad to be of help.
May i also check, my hosts is complaining about max account executions exceeding . . , my current setting is cache 1 min will changing it to 10 mins help? ( although i am not sure if its this plugin causing the heavy usage )
Hard to say without more details. What exactly did they say? Also, how many visitors does your site get per day in average?
It says exceeding script / program executions on the website on a per 24 hour basis.
this website averages about 200 per day . .
That’s not a lot of information haha (I know, it’s not your fault).
With 200 visits per day your site shouldn’t be using that much resources to warrant a warning from your hosting provider IMO (unless your hosting plan is really, really limited). You’ll have to reach out to them and ask them which scripts are consuming too much server resources.
Noted with thanks . .
In the event I am desperate and trying all ways and means.. ( your plugin is superb of cause don’t get me wrong ) . . Increasing the cache time will be the right step ya?
That shouldn’t be necessary in my opinion. Your traffic isn’t high enough yet to stress the database server so 1 minute should be fine.
Ok thank. Hopefully we can keep in touch. So that u can guide me on more WordPress stuff ( when u r not busy of cause )
Thank u and good day buddy
Well here in the forum I only provide support for my plugins. If you want my help with WordPress in general you’ll need to contact me privately. Alternatively, you can also post in the general forums and someone will surely step in.
- The topic ‘Possible to assign popular posts to a tag or category?’ is closed to new replies.