Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Reset a meta_key value of WP-Post views to a custom value WordPress

  • SOLVED

Here is my scenario. I have the WP-Post views plugin, which record views via meta_key = views, and the meta_value = xxx.

I call the post on my page to display lets say category 276 :

$cat_id='-8';//the category ID
$limit = get_option('posts_per_page');

query_posts(array('category__and'=>array(276),
'showposts'=>8,'more' => $more = 0,
'meta_key' => 'views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
));


Which works great. But the main thing i want to do, is to display the most viewed posts of the last week or month. This plugin cannot do that because it does not record data, or so i've read.

So why don't i want it that way? Well, the first 8 posts of that category will always show the most viewed, so its pretty static to me. If i come back next week, its still those same highest viewed posts. I want it to be more dynamic.


The only possible solution i can think of is if i reset the "views" value in SQL. Since i want only a week's top posts, i only care about that number of views that post made in a week, nothing more.

But if i do reset the "views" key to 0, not that i know how, but logic tells me that the whole page will be empty because my query command relies on the meta_key views to be populated. Therefore, nothing displays untill it is populated with views. This breaks the design of my page, because i have content <strong>below</strong> this category that depends on that space to be populated by <strong> 8 posts</strong>.

So my point is there <strong>must </strong> be 8 posts showing at all times, therefore i cannot reset the "views" key to 0.

Suddenly it struk me that perhaps if i can reset all the "views" key of the database to "1", it would work!

So that's what i need. I want the "views" key to reset each week to 1, or 5 or which ever low value.

I prefer it to be automatic each week via a command or SQL query, php command, or a scheduled run or something, but i dont know how to make a "cron job", or a button in my admin dashboard that will run that.

Or, if you can find another creative solution, great.

Answers (3)

2012-09-12

Abdelhadi Touil answers:

Hi.
I highly recommend you to use post-views rather than WP-PostViews:
[[LINK href="http://wordpress.org/extend/plugins/post-views/"]]http://wordpress.org/extend/plugins/post-views/ [[/LINK]]
It's very wel and give statistic too, support cache and can import data from WP-PostViews, so you can just install it and uninstall the other one.
I was always using WP-PostViews, but I didn't like the most viewed function like you, and I was looking for a plugin that can display most viewed posts last day or week or month... so post-views was the magic solution for me.
Here in this page you can find how to use it:
[[LINK href="http://ziming.org/dev/post-views"]]http://ziming.org/dev/post-views[[/LINK]]
I think it's the perfect alternative to WP-PostViews.
Good luck.

2012-09-12

Sabby Sam answers:

Hi there,
You have many simple code available in market in which you can implement easily. The best way is that if you use the cpanel hosting then it would be easiest way.

Or
you can use this one
http://www.phpjobscheduler.co.uk/

or
search for custom cron job scirpt.

once you done with this you can easily implement your query in php file.
I hope this will make sense.



2012-09-12

Pixel Coder answers:

Hello, hope that gets you on the right path.

// Run this when WordPress fires its "init" method
add_action('init', 'px_view_init');

function px_view_init()
{
// Make sure this event only gets scheduled once
// WordPress will recur again after expiration
if(!wp_next_scheduled('px_views_reset')) :
wp_schedule_event(time(), 60 * 60 * 24 * 31, 'px_views_reset');
endif;
}

// By now the event has been scheduled, when it's time to run
// The next function will set your meta value
add_action('px_views_init', 'px_view_reset');

function px_views_reset()
{
// "views" represents the meta field for the plugin
// "0" represents the value you wish to save
update_post_meta($post->ID, 'views', '0');
}


monitor comments:

Do i put that in my functions.php file?, I'm not sure when it fires its init method. When i add <?php update_post_meta($post->ID, 'views', '1'); ?> to the top of my single.php file, it works.

But that means i have to open each post for it to be set to 1. I want all posts set to 1 simultaneously without having to open them.