Answer the question
In order to leave comments, you need to log in
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();
//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
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 questionAsk a Question
731 491 924 answers to any question