Home > OS >  How to move all the posts to move to trash
How to move all the posts to move to trash

Time:01-05

I'm facing the issue in route change by click the apply button, while selecting the move to trash.

NOTE: move to trash and apply process is working by selecting some posts. Bulkly, I trying to move to trash is facing the issue.

url : https://abcd.com/blog/probs/wp-admin/edit.php

After selecting move to trash without selecting any posts and click the apply button url changes to

Incase of delete all posts, I got a route like this

changed url : https://abcd.com/probs//wp-admin/edit.php?paged=1

CodePudding user response:

For this you can try it programmatically by creating your code.

for trashing all the post you try this code:

function move_all_posts_to_trash() {
    $args = array(
        'post_type' => 'post', // Change this to the post type you want to move to trash
        'posts_per_page' => -1, // Get all posts
        'post_status' => 'publish' // Only get posts that are published
    );
    $posts = get_posts( $args );
    foreach ( $posts as $post ) {
        wp_trash_post( $post->ID ); // Move post to trash
    }
}

CodePudding user response:

// If you want to move single post or page you can used by default wordpress function

Basic Example

Trash the default WordPress Post, “Hello World,” which has an ID of ‘1’.


<?php wp_trash_post( $post_id = 1 ); ?>


// For Multiple Posts using WP_Query using while loop.

You can used as per requirement like create shortcode or fire hooks.

function trash_custom_posts() { 

 $args = array (
    'post_type' => 'custom_post_type_slug',
    'post_status' => 'publish', 
    'posts_per_page' => -1
 );
 
 $query = new WP_Query( $args );
 
 while( $query->have_posts() ) {
 
    $query->the_post();
    
    $post_id = get_the_ID();

    wp_trash_post( $post_id );
 }
 
 wp_reset_postdata();
 
 }
 
 add_action( 'init', 'trash_custom_posts' );  
 

  • Related