Last active
June 13, 2018 08:25
-
-
Save mikeyhoward1977/9206826531141ca2448e4a6d1897a5d9 to your computer and use it in GitHub Desktop.
KB Support: Search for tickets with an orphaned status and update
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Find tickets that are set to a status that is no longer registered | |
* and update their status to the value of the $new_status variable. | |
* | |
* @written by Mike Howard | |
* @for KB Support - https://wordpress.org/plugins/kb-support | |
*/ | |
function kbs_update_orphaned_ticket_status() { | |
global $wpdb; | |
$new_status = 'hold'; | |
$valid_status = get_post_stati(); | |
$status_keys = ""; | |
$i = 0; | |
foreach( $valid_status as $valid ) { | |
if ( $i > 0 ) { | |
$status_keys .= ','; | |
} | |
$status_keys .= "'{$valid}'"; | |
$i++; | |
} | |
$orphans = $wpdb->get_results( | |
" | |
UPDATE $wpdb->posts | |
SET post_status = $new_status | |
WHERE post_type = 'kbs_ticket' | |
AND post_status NOT IN ($status_keys) | |
" | |
); | |
} | |
add_action( 'admin_init', 'kbs_update_orphaned_ticket_status' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment