Forums › Forums › Search & Filter Pro › Will the cache rebuild automatically?
Tagged: cache, cron, WP Allimport
- This topic has 16 replies, 5 voices, and was last updated 7 years ago by Trevor.
-
Anonymous(Private) November 8, 2017 at 3:21 pm #140712
I have a similar problem…
My site is a list of events, each with a custom field “event_date” which determines the start date and time of the event. 2 hours after the “event_date”, the event no longer appears in the list.
Some of the events however are weekly events so I have a cron job that runs each night checking for any weekly events that have expired and adding another week to the “event_date” field directly in the database which republished them on my old site.
Although the value in the database is updated successfully, these events will not appear in the “Search & Filter” results I use on my new site unless I manually rebuild the cache.
I was hoping it would be as simple as another cron job to rebuild the cache but reading this post, it doesn’t look like it’s going to be that easy? Is there any way of automatically rebuilding the cache each night?
Thanks,
Mark.Trevor(Private) November 8, 2017 at 3:53 pm #140728It rather depends on how your cron job works. It should access each post one at a time … do its checks, update etc. It should be a function that returns some form of confirmation that the operation on post ID xxx succeeded, probaly with two return vars, the ID and operation status. At that point, you should run the Update Cache for a Particular Post action, using that returned Post ID.
Anonymous(Private) November 13, 2017 at 2:42 pm #141640The cron I have running is as follows…
UPDATE wp_postmeta rh INNER JOIN ( SELECT ID, wt.term_id FROM wp_terms wt INNER JOIN wp_term_taxonomy wtt ON wt.term_id = wtt.term_id INNER JOIN wp_term_relationships wpr ON wpr.term_taxonomy_id = wtt.term_taxonomy_id INNER JOIN wp_posts ON ID = wpr.object_id WHERE taxonomy = 'category' AND (wt.term_id=18) AND post_type = 'post' ) t ON rh.post_id = t.ID SET rh.meta_value = rh.meta_value + CASE WHEN t.term_id = 18 THEN 604800 END WHERE rh.meta_key = 'event_date_time' AND rh.meta_value < UNIX_TIMESTAMP();
It doesn’t return any confirmation.
Is there no way of just rebuilding the whole cache automatically once a day?
Ross Moderator(Private) November 13, 2017 at 6:26 pm #141686Hi there
The problem is, even wordpress some caching mechanisms built into it.
So, modifying the database directly is never recommended.
I’ve experienced this before, updating a value in the DB directly, then using a WP function like
get_post
and finding that some data there is not properly updated.S&F always updates your posts on the fly after the initial build of the cache, so you shouldn’t need to be running this if you are managing your posts the WP way, and using their wrapper functions etc (like wp_update_post, wp_set_post_terms, update_post_meta).
So there are two ways I would suggest. Either:
1) Change your cron job, to properly use the WordPress functions for updating post data (I think you will end up having to do this)
or
2) Use our action for telling S&F to update its cache for a particular post, in your cron job, repeating it for all posts that are affected by your changes, and only after the changes have been made and updated.
do_action('search_filter_update_post_cache', 23);
However, I’m not sure this will work, as WordPress may need to run some internal functions, which S&F then uses to get the data related to your posts.
Thanks
Anonymous(Private) November 15, 2017 at 12:41 pm #142093Thanks for that. Wasn’t too sure where to start so I decided using wp_update_post for every post in category 18 (Weekly) would be easier. (Coding is definitely not my strong point!)
Just for the benefit of anyone else in my situation, I added the following (which I found online somewhere and hacked) to the end of the existing cron job (above) and it seems to have worked…
$postArgs= array( 'category' => 18, 'posts_per_page' => -1 ); $postReq= new WP_Query(postArgs); if($postReq->found_posts) { foreach($postReq->posts as $my_post) { $updated_post_info = array( 'ID' => $my_post->ID, 'pinged' => '' ); wp_update_post($updated_post_info); } }
I’ll keep my eye on it to make sure it’s not doing anything else untoward.
-
AuthorPosts