php – remove older posts
-
Hello everyone. Still somewhat new. I am creating a site with posts that have headlines and has news links back to the original source created by an aggregator from pre-determined words that I specify.
Here is my problem, the links/titles are generated, but stick around for quite some time. I am not live, but I found a number of posts that were still on the front page fron January.
I need to be able to have posts deleted/archived what ever after x days. Let’s say 2 days. If a post comes in on Monday, it is automatically deleted/archived on Wednesday, etc.
I have been pointed to WP Expirator plugin, which doesn’t seem to be updated and have read mixed reviews. So was thinking could I do this in a cron job which I have no clue what to do? Next was looking at wp snippets and using php. I found this code and it seems to work, but I believe I have to delete/archive manually. Is there a way to make this delete/archive automatically. And am using this through the snippets plugin:
$days = 2; $dir = dirname ( __FILE__ ); $nofiles = 0; if ($handle = opendir($dir)) { while (( $file = readdir($handle)) !== false ) { if ( $file == '.' || $file == '..' || is_dir($dir.'/'.$file) ) { continue; } if ((time() - filemtime($dir.'/'.$file)) > ($days *172800)) { $nofiles++; unlink($dir.'/'.$file); } } closedir($handle); echo "Total files deleted: $nofiles \n"; }
I am new enough to know that I don’t know where I am going. Any suggestions would be greatly appreciated.
Thanks,
Elliot
- The topic ‘php – remove older posts’ is closed to new replies.