Hot answers tagged

26

WP Cron runs, when somebody visits your website. Thus if nobody visits, the cron never runs. Now there are 2 solutions: Disable WP Cron, use a real cron job and customize it. https://support.hostgator.com/articles/specialized-help/technical/wordpress/how-to-replace-wordpress-cron-with-a-real-cron-job Use a custom interval in wp_schedule_event(): ...


21

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 the wp_schedule_event function would look like this: wp_schedule_event( strtotime('16:20:00'), 'daily', 'import_into_db' );


19

Thanks Privateer for the prompt reply and advice. I found a way around it before I saw your answer. Here is a step-by-step method for deleting thousands of old cron jobs and may be of use to someone else. I logged on to phpMyAdmin. I clicked on my database and then the 'search' tab. I typed in 'cron' then selected 'all tables' and clicked 'Go'. I scrolled ...


16

I think the best way is to use WP-CLI but you'd need to write a bash script to do this. Here is one that should do it for you: WP_PATH="/path/to/wp" for SITE_URL in = $(wp site list --fields=domain,path,archived,deleted --format=csv --path="$WP_PATH" | grep ",0,0$" | awk -F ',' '{print $1 $2}') do for EVENT_HOOK in $(wp cron event list --format=csv --...


15

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 --path flag when calling cron run. wp cron event run --due-now [<hook>…] One or more hooks to run. [--due-now] Run all hooks due right now. [-...


14

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 explicitly include them by adding include_once( ABSPATH . 'wp-admin/includes/image.php' ); into your call_import function.


11

Short answer - Nope. Any page request initializes the scheduled queue. It's just an initialize request. Wp-cron request is a standalone request. so requesting URL /somepage you just initialize request to /wp-cron.php However - If cron event doesn't work really well (it's has 1000 db queries e.g. or its requesting a some really long-to-respond resource), ...


11

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

Try SELECT * FROM `wp_options` WHERE option_name = 'cron' If you find it you might try: In SQL: UPDATE wp_options SET option_value = '' WHERE option_name = 'cron' In wordpress: update_option('cron', ''); You might need to either delete the cron option or set the value to an empty serialized array. Using update_option would be safer as I'm not certain ...


9

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 timeout - see https://wordpress.org/support/topic/save-a-full-second-on-cron-execution/. Secondly, the spawned job will now be competing with the page load for ...


7

Turns out I had to set the ALTERNATE_WP_CRON flag in wp-config.php to make this work: define('ALTERNATE_WP_CRON', true);


7

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; } error_reporting( 1 ); do_action( 'this_is_cron_event_hook' ); die(); } ); And by going to your website's address: http://example.com?...


6

How wp-cron.php behaves depends on how you execute it. If you execute it by HTTP request with 'doing_wp_cron' in the GET string, it will check if another wp-cron process has set a lock and exit if it has. By default, this is how WordPress executes it. It is easily done in crontab like so: */10 * * * * /usr/bin/wget -q -O "http://www.example.com/wp-cron.php?...


6

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) plugin that only runs whenever you activate it: <?php /** Plugin Name: Clean Cron */ register_activation_hook( __FILE__, function() { delete_option( 'cron' ...


6

This one is actually surprisingly simple; add this to your wp-config.php file and all automatic updates will be blocked when outside of the specified hours: // Suspend updates when outside of business hours, 9:00 AM to 5:30 PM $updates_suspended = (date('Hi') < 0900 || date('Hi') > 1730); define( 'AUTOMATIC_UPDATER_DISABLED', $updates_suspended ); ...


6

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 the category argument and the date_query argument. //* If the scheduled event got removed from the cron schedule, re-add it if( ! wp_next_scheduled( '...


5

onetrickpony's answer is mostly correct. If you let WordPress do its default thing, or you manually trigger wp-cron by fetching its URL with 'doing_wp_cron' set to a unique value in the GET string, then you can reliably expect jobs to not be executed twice. But if you execute wp-cron.php from the PHP CLI in your crontab, the story is different. Now it will ...


5

you can absolutely use wp_cron to specify a time: add_action( 'my_scheduled_event', 'prefix_my_scheduled_event' ); /** * On the scheduled action hook, run a function. */ function prefix_my_scheduled_event() { // do something } //going to use the strtotime function, so a good habit to get into is to set the PHP timezone to UTC default_timezone_set( '...


5

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 +x wp-cli.phar $ sudo mv wp-cli.phar /usr/local/bin/wp You can run your scheduled tasks like so $ wp cron event run --due-now --path=/var/www/mywebsite.com/ ...


5

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

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 other answers.


4

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 don't think just selecting ID of 2100 posts should really eat up 134MB RAM. Just do the math, ID can be saved in just 1 byte, but lets say it's taking 4 bytes. ...


4

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 functions.php: if ( ! wp_next_scheduled( 'expire_posts' ) ) { wp_schedule_event( time(), 'hourly', 'expire_posts' ); } I've used hourly, the other available ...


3

You don't necessarily need to use the WP Cron API for this. Instead you could modify your existing code to make use of WP Transients. Transients are basically Options that expire after a set time. You can wrap your current scrape process in an if statement that checks for the existence of the stored transient. If it exists, it pulls the data from the ...


3

To disable WordPress Cron Jobs, place this in wp-config.php: define('DISABLE_WP_CRON', true); To schedule a cron job in Linux with cPanel for example... This is the command you might run: wget -q -O - http://www.your-domain.org/wp-cron.php?doing_wp_cron >/dev/null 2>&1 The end of the above command just suppresses any output. Just pick the ...


3

When you run things on the CLI (which is pretty much where Cronjob and runwhen jobs are running), then you don't have access to some things like some server or request variable contents, etc. What you need to do is fire up WP Core. Else you won't have access to the full WP API. One method would be to use the wp-cron API from core, but disable WP internal (...


3

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 identifying and understanding the requirements of WP Cron: I noticed events were correctly scheduled and could be run using WP-CLI. And also noticed that ...


3

wp-cli is another way: Listing Events > wp cron event list +-------------------+---------------------+-----------------------+---------------+ | hook | next_run_gmt | next_run_relative | recurrence | +-------------------+---------------------+-----------------------+---------------+ | wp_update_plugins | 2020-04-14 08:11:38 | ...


3

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 allows you to start wget without any path command or if it's allowed to start it anyway. I would first of all try calling example.com/wp-cron.php manually ...


3

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($args); You can use similar way to achieve what you want.


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