get_posts inside cron

Whenever you do get_posts or WP_Query or anything like that, it’s important to remember that the code is actually getting all of the Posts at once, and loading them from the database into memory. If you run out of memory, then your process will simply die with an error. Attempting to get very large numbers … Read more

How to make wp cron job not fire immediately?

As described in wp_schedule_event first parameter is $timestamp – the first time that you want the event to occur. So just add interval to $timestamp. I think it should be like wp_schedule_event( time() + $delay, ‘interval1’, ‘my_cron_hook’ ); And set $delay as miliseconds before hook starts.

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 +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/ or via crontab (every 5mins) */5 * * * … Read more

Delete posts from a post type automatically via Cron

Although I do not understand the motivation for truncating posts, I think this exercise is valuable for you to understand how to use cron + WordPress. Create a function to truncate posts This can be used for both methods below WP-cron or UNIX cron. function foobar_truncate_posts(){ global $wpdb; # Set your threshold of max posts … Read more

Calling static method in the Widget Class

Try this instead: $gloabl $myxclass; $myxclass = new Xwidget(); add_action( ‘xxx_followers’, array(&$myxclass, “cron_addB” ) ); or add_action(‘xxx_followers’, “init_xclass_and_cron”); function init_xclass_and_cron() { $myxclass = new Xwidget(); $myxclass->cron_addB(); }