31
votes
Wp_Schedule_Event every day at specific time
Instead of time(), use strtotime() function so you can specify the time of day - it will use today's date with the time that you specify. So in your case:
strtotime('16:20:00'); // 4:20 PM
Usage in ...
17
votes
Running WP Cron on multisite the right way
After you've added the constant in wp-config.php
defined('DISABLE_WP_CRON') or define('DISABLE_WP_CRON', true);
WP-CLI
And assuming you have your config.yml setup correctly, you can ommit the --...
15
votes
Accepted
Uncaught Error: Call to undefined function wp_generate_attachment_metadata() @ wp-cron
Some of what is usually admin side functionality is not included as part of the "main" wordpress bootstrap, files containing uploaded file manipulation functions are one of them and you need to ...
11
votes
Do WordPress cron jobs slow down page loading?
The short answer is actually yes, in most cases.
Firstly, on most set-ups, spawning a cron job incurs a 1 second delay on page load, because it is done via a loopback HTTP request with a 1 second ...
11
votes
Delete thousands of cron jobs
Wordpress cron events can also be cleared from the command line, using WP-CLI:
wp cron event list
wp cron event delete your_example_event
More details in the wp-cli docs.
10
votes
How to debug WordPress "Cron" wp_schedule_event
You could debug manually, by creating an action and executing the Cron action inside. Like this:
add_action( 'init', function() {
if ( ! isset( $_GET['the_cron_test'] ) ) {
return;
}
...
9
votes
Running WP Cron on multisite the right way
Easier one-liner with less bash:
wp site list --field=url | xargs -i -n1 wp cron event run --due-now --url="{}"
You can either run it manually or put it in a script and call it from cron as in the ...
6
votes
Delete thousands of cron jobs
An even simpler solution is to call delete_option( 'cron' ); once in some plugin. All automatically added cron jobs will get added again on the next visit/request of your site.
As a one case (mu) ...
6
votes
Accepted
What would be a PHP command to erase all posts from category X from the last month?
The first step is setting up the cron job.
The second part requires querying the database for a specific post type where the entry is older than 1 week. We can do this with get_posts() and specifying ...
5
votes
Running wp-cron from CLI
Or, you could use WP-CLI which was developed for scenarios like these. After a short installation like this
$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
$ chmod ...
5
votes
Check if event was scheduled - schedule event only once
Have you ever heard of Rubber Ducking?
The official documentation has an answer:
if ( ! wp_next_scheduled( 'woofio_hourly' ) ) {
wp_schedule_event( time(), 'hourly', 'woofio_hourly' );
}
4
votes
WP Cron Doesn't Execute When Time Elapses
For anyone protecting their (development) site from public access, HTTP Authentication can be the cause of WP Cron not functioning.
In case it can help anyone, here is my list of things I did before ...
4
votes
Using WP_Query and WP_Meta_Query Outside of Wordpress
This is what worked for me:
require_once 'wp-config.php';
global $wpdb;
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
);
$Query= new WP_Query($...
4
votes
Accepted
get_posts / WP_Query Memory size of 134217728 bytes exhausted
If all you want is printing ID in a file, then you may write custom query for it. That way you'll be able to avoid some internal processing WordPress does.
Many posts may exhaust your RAM, although I ...
4
votes
Accepted
Will cron job run if page loaded is being served from cache?
It depends on the plugin and the cache method you are using. For example, as far as I remember, WP Super Cache offers two different cache methods:
PHP Cache
HTML Cache
Using the first method creates ...
4
votes
How do I get a Function in my functions.php to execute with a cron job?
When you use *nix cron to run a PHP file like that it won't have WordPress loaded. You can load it manually, but a better method is to use WordPress' own cron API.
Schedule the event in your ...
3
votes
Accepted
Delete all scheduled events with a particular hook
4.9.0 introduced wp_unschedule_hook()
https://developer.wordpress.org/reference/functions/wp_unschedule_hook/
3
votes
How to test wp_cron?
wp-cli is another way:
Listing Events
> wp cron event list
+-------------------+---------------------+-----------------------+---------------+
| hook | next_run_gmt | ...
3
votes
cron job to auto delete posts of a specific post type older than x days
// add the schedule event if it has been removed
if( ! wp_next_scheduled( 'mg_remove_old_entries' ) ) {
wp_schedule_event( time(), 'daily', 'mg_remove_old_entries' ); //run the event daily
}
// ...
3
votes
Accepted
"Missed schedule" posting bug
You don't really need a Plugin to define these type of cronjobs in a timely manner. All you need to do is to reliably call the Wordpress Cron file which is wp-cron.php
The question is if your Webhost ...
3
votes
Accepted
wp_schedule_event run in background or not?
It depends. The initial page load trigger should not but performance may degrade as visitors browse your site until the Cron job is finished.
Is the scheduled event you plan on using PHP or DB ...
3
votes
Accepted
Avoiding "Missed Schedule" errors when inserting future posts
The missed schedule warning comes form those lines: https://github.com/WordPress/WordPress/blob/f884d7973b052c3efe0901bb136b9898ece8aa39/wp-admin/includes/class-wp-posts-list-table.php#L998,L999
It ...
3
votes
Accepted
How to get woocommerce cart content without an action?
Not tested, but what if you use
WC()->cart->get_cart()
to retrieve the cart contents instead of the global $woocommerce object?
However, I believe the problem is that cart contents require ...
3
votes
Accepted
Code does not work in class implementation but works fine in functions.php file
As Nathan has said in his comment above, the update_events() function needs to be a public function, not a private function:
public scope to make that variable/function available from anywhere, other ...
3
votes
Safe to set permissions to 757 temporarily to update via wp-cli?
I think setting a cron job to automatically turn permissions on and off might be a bit of an extreme workround :-) I think it is probably worth spending the time to set up working permissions on your ...
3
votes
Accepted
How to Trigger WP CRON at Local Timestamp?
Part 1. The Time != The Timestamp
This is your problem:
every midnight at Local Timestamp
There is no such thing as a local timestamp. Timestamps are not timezoned, it's not that WP uses UTC, but ...
3
votes
Accepted
Why is wp-cron only executing on page visit?
It's not possible with just WordPress to schedule code to run in the background at a specific time. WordPress, like most (if not all) PHP web applications, only actually runs in response to a URL ...
3
votes
How to force 'cron_schedules' every minute instead 1 hour?
You have the wrong period alias in your code
You added your personal period in my_cron_schedules() as 61sec:
$schedules["61sec"] = array(
but use it in setup_schedule() as 61sek:
...
2
votes
Running WP Cron on specific local time
First, make sure your WordPress date and time settings are correct as well as your local server time.
Then use this code:
$datetime = new DateTime('midnight', new DateTimeZone(get_option('...
2
votes
WP Cron job every 1st and 15th of the month
I encountered a similar issue but needed to fire an event on the last day of the month. I figured out an alternate solution that would work for this and my issue. Instead of modifying the schedules, ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
cron × 303wp-cron × 158
plugins × 35
php × 34
plugin-development × 34
functions × 13
actions × 11
hooks × 8
email × 7
scheduled-posts × 7
customization × 6
multisite × 6
events × 6
woocommerce-offtopic × 5
mysql × 5
wp-insert-post × 5
wp-cli × 5
transient × 5
custom-post-types × 4
posts × 4
custom-field × 4
options × 4
cache × 4
wp-query × 3
database × 3