I need to trash some custom posts, under certain conditions, via code (it's a plugin self-written)

The theme used is a children

I don't shutdown the ability to trash the post;

In config.php there is not set the constant EMPTY_TRASH_DAYS but in default-constant.php I can read

 * Defines functionality related WordPress constants
 * @since 3.0.0
function wp_functionality_constants() {
     * @since 2.5.0
    if ( !defined( 'AUTOSAVE_INTERVAL' ) )
        define( 'AUTOSAVE_INTERVAL', 60 );

     * @since 2.9.0
    if ( !defined( 'EMPTY_TRASH_DAYS' ) )
        define( 'EMPTY_TRASH_DAYS', 30 );

    if ( !defined('WP_POST_REVISIONS') )
        define('WP_POST_REVISIONS', true);

     * @since 3.3.0
    if ( !defined( 'WP_CRON_LOCK_TIMEOUT' ) )
        define('WP_CRON_LOCK_TIMEOUT', 60);  // In seconds

From dashboard I can trash the custom posts and the single post and page, so trash (seems to me) to work.

But from my code if I select a post to trash, it will be deleted.


$temp = new \WP_Query( $args );

        if ( $temp->have_posts() ) {

            while ( $temp->have_posts() ) {

                $id_post = get_the_ID();

                wp_delete_post($id_post , false);




If I try with wp_trash_post, posts are trashed and not deleted.

Any idea? Thank you very much

  • Check the config.php file and be sure that EMPTY_TRASH_DAYS constant is not set to zero. Also, deactivate all plugins and activate a default theme and try your code again. Your code looks correct. – cybmeta Nov 28 '17 at 8:18
  • @cybmeta I'm using last version of WP, 4.9. The constant is not configured on config.php but it is present on default-constant.php (you can see in my post). I did try of course with base theme and no plugin other than mine. – sineverba Nov 28 '17 at 8:43
  • Can you verify that $id_post is correct? In the way you are using get_the_ID(), you should do $temp->the_post() first. Can you share a full sample code that we can use directly to try to reproduce the problem? – cybmeta Nov 28 '17 at 11:40
  • I did a mistake. If I try with wp_trash_post, posts are trashed. Custom Posts are totally deleted If I use wp_delete_post, also with "false" param. – sineverba Nov 28 '17 at 14:56

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.