WordPress ·

The WordPress Trash: An Underrated Feature

The WordPress Trash: An Underrated Feature
How to use the WordPress trash

WordPress has a recycle bin management system that we've probably never given the importance it deserves. Keep reading and you'll discover a good number of highly valuable facts.

Automatic emptying of the WordPress trash

As explained in the WordPress documentation, the WordPress trash is emptied every 30 days, but this period can be modified using a plugin or by inserting a simple line of code in our wp-config.php file:

define( 'EMPTY_TRASH_DAYS', 1 );

The wp-config.php file is located in the root folder of our WordPress installation, which we can access via FTP, SSH or our server's file manager (if one exists).

EMPTY_TRASH_DAYS is a constant predefined in WordPress with a value of 30. In the code above we're changing this value to 1 so that, as you've probably guessed, items remain in the trash for just 1 day. After that, they'll be automatically deleted.

Where is the WordPress trash?

The trash is available separately in different areas of the WordPress dashboard — in comments, posts and pages, for example. This means each has its own independent trash where deleted items are temporarily stored.

Logically, this data is stored in the wp_posts table of the database, under the column post_status = trash.

If you want to customise the appearance of your website beyond basic options, you can also add additional CSS in WordPress to modify specific styles without altering your theme files.

Recovering items from the trash

If we have an item in the trash and want to recover it, we can do so from the same dashboard. For example, if we're in the pages section, we'll need to click at the top of the list on Trash.

There we'll see all pages in the recycle bin. If we hover the mouse over one of the items in the list, we'll see two links appear: Restore and Delete permanently. We can do either by clicking on whichever one we consider most appropriate.

We should be aware that if we recover an item from the trash, it will appear as a draft and not as published.

Permanently deleting pages, posts or comments

If we want these items to be permanently deleted and not stored for a period of time, we can modify the code explained above. If we set the number of days to 0, we'll prevent them from being temporarily stored, like so:

define( 'EMPTY_TRASH_DAYS', 0 );

The trash doesn't appear in WordPress

If we can't find the trash, it's very likely that there are no items associated with it. If, for example, we add a page to the trash and it doesn't appear, it may be that the predefined automatic deletion period has already passed. We should also make sure we're looking in the right section — pages versus posts, for instance.

Enabling trash for images and media

In WordPress, for images and media, the option to send items to the trash does not exist by default. Instead you'll only find the option "Delete permanently".

If we want the option to send images and media to the trash, we can do so by adding a small piece of code to our wp-config.php (yes, the same file I explained above).

define( 'MEDIA_TRASH', true );

By default, this constant is set to false. That is, it's off by default — presumably to avoid storing unnecessary and large files.

Once we've added the code in wp-config.php we can verify that

Advanced use of the trash in WordPress

Advanced view of how the WordPress trash works

If we understand how WordPress works, we know that all our posts and pages are stored in a database table called posts (with its prefix, of course). That table stores the most relevant data, such as the status. And this status — a column in our table — is what gets modified when we send items to the trash.

That is, a post that has been sent to the trash will appear with post_status as trash. Knowing this, we can use whatever SQL code we see fit. For example, to delete all pages and posts that are in the trash we can write:

DELETE FROM posts WHERE post_status = 'trash';

Of course, we need to add our website's prefix to the table name posts. By default in WordPress, this prefix is wp_.

Conclusion and recommendations

It's true that the trash functionality can be very useful for the vast majority of WordPress websites. But if we have a large number of pages, posts or any type of content, we'll be slowing down the website due to information overload in the posts table. And a website that takes a long time to load will negatively affect search rankings.

That is, every time a page on our website is loaded, there will be a delay while the content is retrieved due to the database query. So in these cases, a great option could be to prevent posts from being stored in the trash.

And now you're probably wondering: how can we have that safety net without the trash? Simple — by running permanent backups. If you have a medium-to-high quality server, you'll almost certainly have this service included. If that's not the case, you can always install a backup plugin.

Keep reading

Professional Website Maintenance: How Much Does Skipping It Cost You?

Fast Emails by Configuring SMTP in WordPress

How to Translate Your WordPress Plugin (or Theme)

Comments

Be the first to comment on this post.

Leave a comment

Your email won't be public. Comments are moderated before being published.