1

I know I can do

wp_untrash_post( $post_id );

And check the post status with

$post_status    = get_post_status ( $post_id )

I'm in a situation where I must untrash some posts but only if the post itself was not a simple draft

Is it possibile to check something like a previous state of a trashed post?

  • 2
    As far I know, there is post status tracking, you could implement it using a post meta field. – cybmeta Oct 28 '16 at 10:15
3

Just as @cybmeta mentioned, the info is stored in the post meta table. When we trash a post then the post meta key _wp_trash_meta_status is added with the value of the previous post status (publish, pending, draft, ... ).

Within the wp_trash_post() function, we have:

add_post_meta($post_id,'_wp_trash_meta_status', $post['post_status']);
add_post_meta($post_id,'_wp_trash_meta_time', time());

We might therefore try:

// Get previous post status
$status = get_post_meta( $post_id, '_wp_trash_meta_status', true );

// Only untrash if the previous status wasn't a 'draft'
if( 'draft' !== $status )
   wp_untrash_post( $post_id );

to only untrash a post if it's previous post status wasn't a draft.

Demo Plugin

Here's an example how we can add a sortable Previous Post Status column to the trash view in the post list table in the backend:

View post status in the trash view

We could then use the bulk delete as needed.

Here's a demo plugin to support it:

<?php
/** 
 * Plugin Name: Demo Plugin
 * Description: Adds the 'Previous Post Status' Column in the 'trash' post table view
 * Plugin URI:  http://wordpress.stackexchange.com/a/244261/26350
 */

namespace WPSE\Q244254;

add_action( 'admin_head', function()
{
    // Only target the trash view on the edit.php page
    if( 'trash' !== get_query_var( 'post_status' ) || ! did_action( 'load-edit.php' ) )
        return;

    // Let's start it here
    $o = new Main;
    $o->init( $GLOBALS['wpdb'] );

} );

class Main
{
    private $db;

    public function init( \wpdb $db )
    {       
        $this->db = $db;

        add_filter( 'manage_post_posts_columns',            [ $this, 'columns']                     );
        add_filter( 'manage_edit-post_sortable_columns',    [ $this, 'sortable_columns' ]           ); 
        add_filter( 'posts_orderby',                        [ $this, 'orderby' ],           10, 2   ); 
        add_action( 'manage_post_posts_custom_column',      [ $this, 'custom_column' ],     10, 2   );
    }

    public function columns ( $columns )
    {
        // Place our new custom column right after the 'title' column
        $_columns = [];
        foreach( (array) $columns as $key => $label )
        {
            $_columns[$key] = $label; 
            if( 'title' === $key )
                $_columns['wpse_prev_post_status'] = esc_html__( 'Previous Post Status', 'mydomain' );     
        }
        return $_columns;
    }

    public function custom_column( $column_name, $post_id ) 
    {   
        // Display the previous post status
        if ( $column_name == 'wpse_prev_post_status' )
            echo get_post_meta( $post_id, '_wp_trash_meta_status', true );
    }

    public function sortable_columns( $columns ) 
    {
        // Make our new column sortable
        $columns['wpse_prev_post_status'] = 'wpse_prev_post_status';
        return $columns;
    } 

    public function orderby( $orderby, \WP_Query $q )
    {
        // Implement the orderby support to our custom column

        $_orderby = $q->get( 'orderby' );
        $_order   = 'ASC' === strtoupper( $q->get( 'order' ) ) ? 'ASC' : 'DESC';

        if( 
            is_admin() 
            && $q->is_main_query() 
            && 'wpse_prev_post_status' === $_orderby 
        ) 
            $orderby .= $this->db->prepare( "{$this->db->posts}.post_status %s", $_order );

        return $orderby;
    }

} // end class

Note that here we target the post post type.

Hopefully you can adjust it further to your needs, like adding a post status filtering.

  • One the best answers I never received. – realtebo Oct 28 '16 at 13:09

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.