0

I'd like to move a post to the trash in the loop. Here's my code;

function run_every_five_minutes() {
    global $post;
    $wp_query = new WP_Query(array('cat' => array(1), 'posts_per_page' => -1, 'date_query'=>array('after' => '2 hours ago', 'inclusive' => true)));
    $post_id = get_the_ID();
    if ( $wp_query->have_posts() ) {
            while ($wp_query->have_posts()) : $wp_query->the_post();
                    wp_trash_post($post_id);
            endwhile;
    }
}

With this function, I'm able to move a post to the thrash but the post still listing on the admin panel(All posts section) and also showing up in the trash. How do I remove it completely?

Edit: The post is private

3
  • what do you call "the posts page" ? edit the question to give more details.
    – Kaperto
    Nov 13, 2019 at 13:46
  • @Kaperto I've just edited the question
    – Kevin S
    Nov 13, 2019 at 13:53
  • look at the post ID before the code. and after deleting, search this ID to find if the duplicate post is in the published list or in the deleted post.
    – Kaperto
    Nov 13, 2019 at 14:02

1 Answer 1

0

As the function name suggests, wp_trash_post() only trashes a post. If you want to have a post completely deleted then you can use wp_delete_post( int $postid, bool $force_delete = false ) instead.

https://developer.wordpress.org/reference/functions/wp_delete_post/

When the post and page is permanently deleted, everything that is tied to it is deleted also. This includes comments, post meta fields, and terms associated with the post.

The post or page is moved to trash instead of permanently deleted unless trash is disabled, item is already in the trash, or $force_delete is true.


EDIT 14.11.2019

Perhaps WP just freaks out, because you're using wp_trash_post for the same $post_id on every iteration of the while loop. Maybe you could test one of the following functions, which I wrote based on your code, depending on whether you want to delete single or all matched posts.

function run_every_five_minutes_delete_one_matched_post() {
  global $post;
  if ( empty($post->ID) ) {
    return;
  }
  $wp_query = new WP_Query(
    array(
      'p'                      => $post->ID, // query only for this particular post
      'date_query'             => array(
        'after'                => '2 hours ago', 
        'inclusive'            => true
      ),
      'no_found_rows'          => true,
      'update_post_meta_cache' => false,
      'update_post_term_cache' => false,
      'fields'                 => 'ids' // no need to query all of the post data
    )
  );
  if ($wp_query->posts) {
    wp_delete_post($post->ID, true); // second parameter true bypasses trash and force deletes the post
  }  
}

function run_every_five_minutes_delete_all_matched_posts() {
  $wp_query = new WP_Query(
    array(
      'cat'                    => array(1), 
      'posts_per_page'         => -1,
      'date_query'             => array(
        'after'                => '2 hours ago', 
        'inclusive'            => true
      ),
      'no_found_rows'          => true,
      'update_post_meta_cache' => false,
      'update_post_term_cache' => false,
      'fields'                 => 'ids'
    )
  );
  if ($wp_query->posts) {
    array_walk($wp_query->posts, function($post_id){
      wp_delete_post($post_id, true); // second parameter true bypasses trash and force deletes the post
    });
  }
}
4
  • Thank you for your answer but I think I couldn't define my problem exactly. So I've uploaded a video to show the problem. Here's the link After a post deleted, it copies to trash not moves it.
    – Kevin S
    Nov 14, 2019 at 8:37
  • Also, there's almost the same question in SO. I've tried this too but no luck.
    – Kevin S
    Nov 14, 2019 at 8:42
  • @KevinS That's strange. I added couple of examples more to my answer, which you could try, just in case WP is just freaking out of the loop you're using. Nov 14, 2019 at 9:56
  • Yay! It worked! Thank you so much for your time. I appreciated.
    – Kevin S
    Nov 14, 2019 at 11:10

Your Answer

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

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