0

I want to delete all post related on taxonomy.

for e.g whenever i delete a taxonomy from back-end all posts of that taxonomy should be deleted.

i tried below code with hook name is delete_term_taxonomy

   <?php
    add_action('delete_term_taxonomy','wp_custom_delete_taxonomy');
    function wp_custom_delete_taxonomy($tt_id) {
           global $wpdb; // add action here


        $get_term_id = $wpdb->get_results("SELECT meta_value, user_id
                                        FROM `wp_usermeta`
                                        WHERE `meta_key`='term_id'
                                        AND `meta_value` =".$tt_id."");

        $post_metattid = $get_term_id[0]->meta_value;
        $user_id = $get_term_id[0]->user_id;

        $get_post_id = get_user_meta($user_id,'post_id',true);

        $delete_all_model = $wpdb->query("DELETE p,pm,tr,d,e 
                                        FROM wp_posts as p 
                                        LEFT JOIN wp_postmeta as pm ON p.id=pm.post_id 
                                        LEFT JOIN wp_term_relationships as tr ON tr.object_id=pm.post_id 
                                        LEFT JOIN wp_term_taxonomy d ON  d.term_taxonomy_id = tr.term_taxonomy_id 
                                        LEFT JOIN wp_terms e ON  e.term_id = d.term_id 
                                        WHERE tr.term_taxonomy_id=".$tt_id."");
  }  
    ?>

it's not remove posts of taxonomy, anyone have idea regarding this.

2
  • You can't delete a taxonomy from the admin - do you mean a term? Commented Oct 14, 2016 at 12:57
  • yes whenever i remove term then all post should be deleted related it. Commented Oct 14, 2016 at 13:02

1 Answer 1

2

First of all we need to find the posts related to our taxonomy term. Then we will delete them one by one by running a foreach loop. Try below code. I hope that will work for you-

add_action('pre_delete_term', 'the_dramatist_delete_posts_on_taxonomy_delete', 1, 1 );
function the_dramatist_delete_posts_on_taxonomy_delete( $term ) {
    $args = array(
        'post_type' => 'post', // post_type
        'tax_query' => array(
            array(
                'taxonomy' => 'post_tag', // taxonomy_name
                'field' => 'id',
                'terms' => $term
            )
        )
    );

    $posts = get_posts($args);

    foreach ( $posts as $post ){
        wp_delete_post( $post->ID, true );
    }
}
5
  • still not working only term are deleted. Commented Oct 14, 2016 at 14:08
  • Can you please provide the post_type and the taxonomy name(not the term name) ?
    – CodeMascot
    Commented Oct 14, 2016 at 14:10
  • i given name of post type and custom taxonomy. Commented Oct 14, 2016 at 14:11
  • Got the solution. Try the updated code. Change the post_type and taxonomy_name if necessary. This code worked for me. I tested it. @sameersheikh
    – CodeMascot
    Commented Oct 14, 2016 at 15:17
  • Check the updated code. @sameersheikh
    – CodeMascot
    Commented Oct 14, 2016 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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