0

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

enter image description here

0

Start 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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.