1

I know, my question is general and here is some similar questions, I checked the all, but I dont find any solution for my problem... I try using the wp_trash_post() in my function.php, but for some reasons this not working for me... (wp_delete_post() also not working)... I working nonstop with hooks and own functions, plugins, so now this very frustrating... Here is my simple code:

function delete_orphan() { // I trying this also: delete_orphan($postid)
global $post;
  if ( 'my_post_type' == get_post_type() ) : // I trying get_post_type( $post->ID ) also, but not working... otherwise in my another hooks perfectly working with blank get_post_type()

        wp_trash_post( $post->ID ); // If I add an exist post->ID manually to this, like 345, also not working...

  endif;
}
add_action( 'init', 'delete_orphan' );

I use these methods (like if ( 'my_post_type' == get_post_type() ), etc.) in my another functions also, and there workings perfectly ... Whats wrong with this? Any help comes in handy, I have read any article about wp_trash_post and wp_delete_post, I try any another format, (like this: https://stackoverflow.com/a/15012796/9565969), none of them workings for me... (I trying use this code on a multisite, but I dont think, this is relevant in the current case)

... #SOLVED:

I try with some solution, and finally works... Interesting, the code, what now working, originally did not worked, when I yesterday tested... (Not the first time for me, that a perfect function not working immadiately in my functions.php... maybe this is a php bug... ) Otherwise I also try use the wp_trash_post() in plugin, but then I get an error associated with wp_insert_terms... This is also not new issue, the not public custom taxonomies still not workings in plugins ...

So the function, what now works for me:

 function delete_xcustom_posts() {  
 $args = array (
    'post_type' => 'xcustom',
    'post_status' => 'publish', 
    'nopaging' => true // if you use 'numberposts' => -1, this equal with your pre_get_posts options (post/page)
 );
 $query = new WP_Query( $args );
 while( $query->have_posts() ) {
    $query->the_post();
    $xcustom_id = get_the_ID();

        wp_trash_post( $xcustom_id );
 }
 wp_reset_postdata();
 }
 add_action( 'init', 'delete_xcustom_posts' );  

Your Answer

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

Browse other questions tagged or ask your own question.