Quantcast
Viewing all articles
Browse latest Browse all 20145

Pre-filter/change date before query is run?

I have a View that lists post in date order. The date is saved in a Custom-Field called "classified-date". In this case, every 2 weeks the date is refreshed. The way I'm doing this is a short code call to this:

add_shortcode('resetlevelchecker', 'resetlevelchecker_func');
function resetlevelchecker_func() {
$postid = get_the_ID();
$datemeta = get_post_meta($postid,'wpcf-classified-date',true);
$datemetaplus = ($datemeta + 1296000);  /** 2 weeks **/
	if ($currenttimesec > $datemetaplus) {   /** If over 2 weeks... **/
	update_post_meta($evpostid,'wpcf-classified-date',time());  /** Reset date to today **/
	}
}

(Probably could be a bit cleaner but it gets the job done)

The problem is that a person lists all posts, while listing it checks and resets the date. But the viewer sees the old results, since the Shortcode is run AFTER the query. The NEXT person to list posts will see the updated result. Not horrible, I can live with this — But in the interest of perfection, I came up with the idea of using a hook instead of a short code.

Would I use: wpv_filter_query? Something like this?:

add_filter('wpv_filter_query','resetlevelchecker_func');
function resetlevelchecker_func() {
$postid = get_the_ID();
$datemeta = get_post_meta($postid,'wpcf-classified-date',true);
$datemetaplus = ($datemeta + 1296000);  /** 2 weeks **/
	if ($currenttimesec > $datemetaplus) {   /** If over 2 weeks... **/
	update_post_meta($evpostid,'wpcf-classified-date',time());  /** Reset date to today **/
	}
}

So now it changes the date Before the query is run? But that doesn't make sense, because how does it know what post it's on before a query is made. I'm thinking the whole code need to be re-thought out but not sure WHEN this is called and how to make it effect the outcome.

Reference:
http://wp-types.com/documentation/user-guides/views-filters/wpv_filter_query/


Viewing all articles
Browse latest Browse all 20145

Trending Articles