A
A
Ahmad Dusmatov2019-11-14 08:50:58
Task Schedulers
Ahmad Dusmatov, 2019-11-14 08:50:58

How to delete posts via cron?

//delete-packages-with-cron.php
<?php
$parse_uri = explode('wp-content', $_SERVER['SCRIPT_FILENAME']);
require_once($parse_uri[0] . 'wp-load.php');

function deletePackagesOlderOneDay()
{
    $args = array(
        'post_type' => 'package',
        'meta_query' => array(
            array(
                'key' => 'package-origin',
                'value' => array('Брест', 'Гомель', 'Гродно', 'Витебск'),
                'compare' => 'NOT IN',
            )
        ),
        'date_query' => array(
            array(
                'before' => '1 days ago'
            )
        )
    );

    $packages = get_posts($args);

    foreach ($packages as $package) {
        wp_delete_post($package->ID, true);
        echo "Post: " . $package->ID . " deleted.<br>";
        sleep(1);
    }
}

deletePackagesOlderOneDay();

function deletePackagesOlderThreeDay()
{
    $args = array(
        'post_type' => 'package',
        'meta_query' => array(
            array(
                'key' => 'package-origin',
                'value' => array('Брест', 'Гомель', 'Гродно', 'Витебск'),
                'compare' => 'IN',
            )
        ),
        'date_query' => array(
            array(
                'before' => '3 days ago'
            )
        )
    );

    $packages = get_posts($args);

    foreach ($packages as $package) {
        wp_delete_post($package->ID, true);
        echo "Post: " . $package->ID . " deleted.<br>";
        sleep(1);
    }
}

deletePackagesOlderThreeDay();

I run this file via cron every hour. After deleting 5-6 posts, the script stops running, although there are many entries in the selection. Where is the mistake ?
Also added a second option. Cron in theme's functions.php file
//cron for delete packages
$cronArgs = array(false);
if (!wp_next_scheduled('package_hook_1', $cronArgs)) {
    wp_schedule_event(time(), 'hourly', 'package_hook_1', $cronArgs);
}

add_action('package_hook_1', 'deletePackagesOlderOneDay', 10, 1);

function deletePackagesOlderOneDay()
{
    $args = array(
        'post_type' => 'package',
        'meta_query' => array(
            array(
                'key' => 'package-origin',
                'value' => array('Брест', 'Гомель', 'Гродно', 'Витебск'),
                'compare' => 'NOT IN',
            )
        ),
        'date_query' => array(
            array(
                'before' => '1 days ago'
            )
        )
    );

    $packages = get_posts($args);

    foreach ($packages as $package) {
        wp_delete_post($package->ID, true);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Klepko, 2019-11-14
@dusmatoff

in the get_posts() parameters, you need to pass the numberposts parameter.
by default it is 5.
https://wp-kama.ru/function/get_posts#numberposts-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question