Support » Plugins » Hacks » How to auto remove empty post using wp_schedule_event

  • I’m trying to make a simple plugins to remove empty post every hour or day using wp_schedule_event to make it auto.

    I’m already test it activation and deactivated plugins with no problems. The problems is the plugin not working to remove empty post.

    register_activation_hook(__FILE__, 'my_activation');
       add_action('my_hourly_event', 'do_this_hourly');
    function my_activation() {
       wp_schedule_event( time(), 'hourly', 'my_hourly_event');
    function do_this_hourly() {
       // do something every hour
       $wpdb->query('DELETE FROM wp_posts WHERE post_content=""');
    register_deactivation_hook(__FILE__, 'my_deactivation');
    function my_deactivation() {


    Thank you in advance,

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    If this is a test site and not a live installation, scheduled tasks do not run unless someone makes a page request. Other than that, I don’t see what may be the issue.

    I will assume that ‘wp_’ is the correct table prefix for your site. It’s still a good idea to use $wpdb->posts instead of hard coding the prefix. Also, if your theme uses the nav menu feature, you should add AND post_type!="nav_menu_item" to your query.

    I test on live blogs with already have a post with title only.
    Thank you for replies but I still confused with your answers. Can you make it detailed? I’m very confused about this.

    Moderator bcworkz


    Sorry for being confusing, it’s difficult to know how much experience various people have.

    1. Test sites. Scheduled tasks are triggered when someone makes a request for any page in WP. On a live site, this is not a problem because live sites should have users and bots visiting all the time. On local test sites, the only user is the tester, so unless the tester views a page soon after when the task is scheduled, the scheduled task will not run.

    2. Table prefixes. When you install WP and are configuring the wp-config.php file, you can specify a unique table prefix. It defaults to “wp_”, but you can change it to anything, perhaps your’s could be “zp_” for example. Then, to access the posts table, the table name passed to mySQL would need to be “zp_posts”, not the default “wp_posts”. When writing plugins, authors cannot know what prefix any particular installation is using. This is why the $wpdb object builds the correct table name and stores it as a property. When we use the property $wpdb->posts, the correct table name is passed to mySQL, no matter what it really is, whether “zp_posts”, “wp_posts”, “bc_posts”, using $wpdb->posts works correctly for all of them.

    3. Nav Menus. Many themes utilize the built in nav menu capability to allow WP admins to build custom navigation menus for their site. This is typically done in the Appearance>Menus admin panel. Each menu item defined here is actually stored as a post in the posts table. The content field of these special post types are empty. If your SQL query were to run on an installation using the nav menu feature, it would delete all defined menu items. To avoid doing this, you must add the above mentioned condition so that these menu items are not deleted.

    Actually, you cannot know what sort of custom post types someone may use that has no content, so it’s best to specify only which posts types should be deleted, instead of what should not. Of course, this only applies when sharing your plugin with others. For your own site, as long as you know you are not deleting needed information, you can do what only works for your site and not worry about others.

    This all may or may not actually solve your actual problem though. Even so, I hope you are now at least a little less confused and a bit more knowledgeable 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to auto remove empty post using wp_schedule_event’ is closed to new replies.