admin管理员组文章数量:1415673
looking for some assistance.
I have searched the usual sites but not come across this specific question and was wondering if anybody could help. I have a scrape system that collects data and saves it to my database in wordpress. It is saving the track history for a radio station. Unfortunately I am getting double ups which I can't fix. I'd like to run a cron job that reviews the last 30 entries for that custom post type (track) and if it finds any duplicates it deletes all but the oldest.
The image shows an example of the issue.
The tracks highlighted in blue need to be deleted automatically by the system as they aren't needed.
So in summary Cron needs to run every 15 minutes. It needs to look for the last x entries (maybe 30). It needs to look for any duplicates. It needs to delete all but the oldest of those that it finds (that keeps the information about when it first played). This should be a very light job to avoid high cpu.
Any help would be much appreciated.
Thanks
looking for some assistance.
I have searched the usual sites but not come across this specific question and was wondering if anybody could help. I have a scrape system that collects data and saves it to my database in wordpress. It is saving the track history for a radio station. Unfortunately I am getting double ups which I can't fix. I'd like to run a cron job that reviews the last 30 entries for that custom post type (track) and if it finds any duplicates it deletes all but the oldest.
The image shows an example of the issue.
The tracks highlighted in blue need to be deleted automatically by the system as they aren't needed.
So in summary Cron needs to run every 15 minutes. It needs to look for the last x entries (maybe 30). It needs to look for any duplicates. It needs to delete all but the oldest of those that it finds (that keeps the information about when it first played). This should be a very light job to avoid high cpu.
Any help would be much appreciated.
Thanks
Share Improve this question asked Aug 18, 2019 at 22:10 vinyllickervinyllicker 111 Answer
Reset to default 0Start by ensuring that there is actually a 15min interval available.
// create the cron scheduled interval to be used
add_filter('cron_schedules','ex11_cron_schedules');
function ex11_cron_schedules($schedules){
if(!isset($schedules["15min"])){
$schedules["15min"] = array(
'interval' => 15*60,
'display' => __('Once every 15 minutes'));
}
return $schedules;
}
Then create a custom WP_Query loop to fetch all the records you need to compare, and archive those you no longer need (your duplicates):
// I haven't tested this yet. You might need to fine tune it.
// You might also need to tune the query statements to suit the Track CPT
function wpse_archive_duplicate_tracks(){
$args = array( 'post_type'=>'track','pust_status'=>'publish');
$tracks = get_posts($args);
$tracks_to_keep = array();
foreach($tracks as $track){
if (array_key_exists($track->artist . '~' .$track->post_title, $tracks_to_keep)){
$toUpdate = array(
'ID' = $track->ID,
'post_status' = 'archive', // change to 'trash' to delete
);
wp_update_post($toUpdate);
} else {
$tracks_to_keep[$track->artist . '~' .$track->post_title] = $track;
}
}
Finally, you need to schedule your archival function:
wp_schedule_event(time(), '15min', 'wpse_archive_duplicate_tracks');
Parting thought: You might also want to consider adding a meta_data field to the tracks you have already checked, so you don't have to check them ALL every time. But ... this might not suit your situation, if duplicates can come back anytime.
Good luck!
本文标签: cronwp delete duplicate entries of custom post types every 15 minutes
版权声明:本文标题:cron - wp delete duplicate entries of custom post types every 15 minutes 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745209972a2647823.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论