Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
Getting close ( i think ) seems someone else has been looking at the same issue and come up with this: DELETE tr FROM wp_term_relationships tr INNER JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.taxonomy != 'link_category' AND tr.object_id NOT IN (SELECT ID FROM wp_posts); which sort of works by cleaning out some orphaned records, but my dropdowns are still populated with the orphaned terms, im struggling to see how these are associated to a post id which would make it a heck of a lot easier to find a way around –  MartinJJ Jun 21 '11 at 9:51
    
Ok this is where i am now, but im still dubious at how these terms are assigned to wp_posts... DELETE tr FROM wp_term_relationships tr INNER JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.taxonomy = 'location' OR 'property_type' OR 'region' OR 'sale_rental' OR 'price' AND tr.object_id NOT IN (SELECT ID FROM wp_posts); –  MartinJJ Jun 21 '11 at 11:10

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
Gotchya,,, this wordpress thing has so many things to help that often the simplest solutions are already built in.. deffo need to spend more time knowing all these functions.. so going by the answer you have given i have updated my original post with a code solution to use wp_delete_posts am i know going on the right lines? –  MartinJJ Jun 22 '11 at 9:18
    
Looking much better :) You're right, WordPress is pretty big, and it's not always easy to know what's already available for you. It does follow some sort of pattern though, so there are wp_insert_TYPE and wp_delete_TYPE functions for terms, posts, links, comments & users. And unless you're doing a really complex custom query, there's usually a function for it already - just ask here, browse the codex, or start googling, and you'll soon find it ;) –  TheDeadMedic Jun 22 '11 at 10:05
    
all advice taken onboard, many thanks TDM... marked as answered –  MartinJJ Jun 22 '11 at 10:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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