Hot answers tagged

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 ...
Goran Jakovljevic's user avatar
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 --...
jgraup's user avatar
  • 9,699
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 ...
Mark Kaplun's user avatar
  • 23.2k
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 ...
Jake's user avatar
  • 211
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.
Paul Wenzel's user avatar
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; } ...
WPMaster's user avatar
  • 101
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 ...
Anastis's user avatar
  • 546
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) ...
kaiser's user avatar
  • 50.4k
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 ...
Nathan Johnson's user avatar
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 ...
kero's user avatar
  • 6,170
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' ); }
Pavel Janicek's user avatar
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 ...
sampi's user avatar
  • 159
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($...
Aniruddha Gawade's user avatar
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 ...
Fayaz's user avatar
  • 8,819
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 ...
Johansson's user avatar
  • 15.1k
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 ...
TheDeadMedic's user avatar
  • 36.2k
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/
Jason Rikard's user avatar
3 votes

How to test wp_cron?

wp-cli is another way: Listing Events > wp cron event list +-------------------+---------------------+-----------------------+---------------+ | hook | next_run_gmt | ...
Ian Dunn's user avatar
  • 4,797
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 } // ...
MagniGeeks Technologies's user avatar
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 ...
Helmi's user avatar
  • 116
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 ...
cowgill's user avatar
  • 1,072
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 ...
david.binda's user avatar
  • 3,358
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 ...
Shawn H's user avatar
  • 671
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 ...
Mat's user avatar
  • 1,616
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 ...
junkrig's user avatar
  • 448
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 ...
Tom J Nowell's user avatar
  • 57.6k
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 ...
Jacob Peattie's user avatar
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: ...
burlakvo's user avatar
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('...
Tyler's user avatar
  • 21
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, ...
Aaron's user avatar
  • 21

Only top scored, non community-wiki answers of a minimum length are eligible