miniMAC miniMAC - 4 months ago 13
PHP Question

How to var dump each posts when moving to trash in wordpress

I'm trying to show a list of posts inside a var_dump, this is the actual code:

function deleted_cpt_orders() {

global $post_type, $post;

if ( $post_type == 'cpt_orders' ) {

var_dump($post);

}

}
add_action( 'trashed_post', 'deleted_cpt_orders' );


If i delete only 1 post the
var_dump
is shown, but if i delete 2 posts or more the result is

NULL

Answer

global $post is supposed to hold a single post object. If you delete multiple objects, it won't be set. However, trashed_post hook passes post id, therefore, you can do the following

function deleted_cpt_orders($object_id)
{
    $post = get_post($object_id);
    if ($post->post_type == 'cpt_orders')
    {
        var_dump($post);
    }
}

add_action('trashed_post', 'deleted_cpt_orders');