I have 3 custom post types each of these CPT’s have posts that are set to auto delete via a given time scale by cron jobs, thing is when they are deleted it leaves behind orphaned taxonomy terms in dropdowns etc, needless to say when these are clicked it goes to a “quack quack oops 404 error”, i can find sql queries to run for orphaned tags, but there seems to be very little (infact i have found nothing at all) that pertains to taxonomy terms, anubody have any ideas on this?
More Info
All my terms are pre-assigned to the taxonomies and are listed in wp_terms table, the reason they are pre-assigned is that authors choose them from option menus when creating a new post, so all un-attached (un-used terms) still need to stay in the wp_terms table for later inclusion, the tables i need to be getting info from to run the delete query are wp_terms_taxonomy, wp_terms_relationships and wp_posts to look for a match of a NULL post id and thats the part what stumps me.
I am showing the terms for selection via Scribus Query Multiple Taxonomies plugin.
UPDATE:
From theDeadMedics answer below im deducing that i can do something on the lines of:
<?php
require_once 'wp-load.php';
global $wpdb;
$expireds = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'sales' AND HOUR(TIMEDIFF(NOW(), post_date_gmt)) >=4321
");
foreach ($expireds as $expired) {
wp_delete_post( $post->ID, true );
}
?>
LAST UPDATE
i see a couple of folks have set this as a favourite, below is a full working query that will delete time expired posts (and all meta associated with that post) set to a “n”th number of hours after post_date_gmt.
In the code below >=2
denotes delete posts 1 hour old, the number of hours you wish to set for delete should always be plus 1.
<?php
require_once 'wp-load.php';
global $wpdb;
$expireds = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'sales' AND HOUR(TIMEDIFF(NOW(), post_date_gmt)) >=1");
foreach ($expireds as $post) {
wp_delete_post( $post->ID, false );
}
?>
Regards
You shouldn’t need to use a custom SQL query – stick to the built-in method
wp_delete_post()
. This will also clear out all term relationships too.