Dears,
I want to delete the old post automatically without using a plugin and the Snippets will be the choice, I want to delete any post older than one day from a special category an using this Snippet but nothing deleted:
// Automatically delete posts older than x days
function delete_old_posts($category_id = 62) {
$days = 1; // Change this value to the desired number of days
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'date_query' => array(
array(
'before' => $days . ' days ago',
'inclusive' => true,
),
),
'tax_query' => array(
array(
'taxonomy' => 'slug', // Replace 'category' with your actual taxonomy slug if different
'field' => 'id',
'terms' => array($category_id), // Array of category IDs to target
),
),
);
$old_posts = new WP_Query($args);
if ($old_posts->have_posts()) :
while ($old_posts->have_posts()) : $old_posts->the_post();
wp_delete_post(get_the_ID(), true);
endwhile;
endif;
wp_reset_postdata();
}
add_action('wp', 'delete_old_posts');
3
Answers
No straightforward solution:
There is no straightforward solution in WP, you have to use custom SQL.
Basic understanding of post and term table and their relationship:
wp_terms
stores term names with Primary Key (PK) associated IDwp_term_relationships
is a relationship table between posts & their taxonomy/termswp_term_taxonomy
is a relationship table between term and their associated taxonomiesSQL:
Caution always first check the SQL through
SELECT
statement before using non-revokable action like deleting records.Select:
WHERE
clause change the value ofwt.slug
fromtemp
to your desired category slug.post_date <= DATE_SUB(NOW(), INTERVAL 6 HOUR)
change the interval to your desired time unit like Week, Day, Hour, etc. (Reference for unit: https://dev.mysql.com/doc/refman/8.0/en/expressions.html#temporal-intervals)Delete:
If you’re satisfied with the output of
SELECT
statement, then change it toDELETE
statement like below.You can use this:
I recommend using it in a child theme instead of a snippet. The hook may not be triggered with the snippet. Or you can call the function directly.
TLDR;
Grab the te_delete_old_posts() function if using independently and no need for the cron parts if that is not required.
Make sure your Query returns the required result:
In the example below I extracted the main query component so you can add to a function and run it.
The date initially passed does not come out as expected. $query_date is used to get the correct date using PHP date/strtotime functions with no time residue.
Also I am telling the query to provide only ID’s of the results so that it will be efficient than getting entire post objects.
Finally I am using get_posts() so that we can get an clean array of Post ID’s to loop thru, and not have to deal with WP_Query().
Running just this code you can verify that you are getting the expected results, and tweak the query if not.
Is this a Recurring event:
If this process is a recurring event which it sounded like from the description. It should go in WP cronjob so that the process does not keep running each time a page loads as you had in the ‘wp’ hook. I am guessing that must have been for testing or you were planning on doing this manually.
Running with WP-CLI
If you do not wish to use a cronjob and want to run this manually, add the function te_delete_old_posts() in your themes function.php or in a custom plugin and you can use one of the two methods bellow to run the function.
Hit enter after typing the function name inside the shell
Always use wp_delete_post() to delete Posts:
Your script is a pretty good start, and you seemed to be on the right track. WordPress creates data in a few tables when you create a posts and it is important that proper cleanup happens when you delete a post. wp_delete_post() does just that.
And as mentioned use the code I provided for testing and make sure you are getting results first, if something is not working.