Cron Job not working in plugin

The cron event needs to be registered on the plugin activation hook like so: register_activation_hook( __FILE__, ‘activate_cron’ ); function activate_cron() { wp_schedule_event( current_time( ‘timestamp’ ), ‘hourly’, ‘the_function_to_run’ ); } The the_function_to_run defined in the wp_schedule_event needs to be your function that you want to run at the time interval. Note: When you say something is … Read more

wp_delete_auto_drafts() deletes links in menus

This is what normal query run by wp_get_associated_nav_menu_items() looks like: SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.id = wp_postmeta.post_id ) WHERE 1 = 1 AND wp_posts.post_type=”nav_menu_item” AND (( wp_posts.post_status <> ‘trash’ AND wp_posts.post_status <> ‘auto-draft’ )) AND (( wp_postmeta.meta_key = ‘_menu_item_object_id’ AND Cast(wp_postmeta.meta_value AS CHAR) = ‘3111’ )) GROUP BY wp_posts.id ORDER … Read more

Cron schedule interval through plugin options?

Yes, in my opinion that’s exactly the right way to extend the default schedule timings. I would be a little more informative with the display value but I think that is only example code. However, one important thing should be taken into account. The default WordPress schedule implementation that is invoked as a side effect … Read more

Where in the page load code is wp-cron triggered?

The WordPress cron is run by the wp_cron() function, which is hooked to run on the init hook, which runs on every page load. wp_cron() is defined in wp-includes/cron.php and hooked in wp-includes/default-filters.php. The wp_cron() function kicks off a wp_remote_post() request to /wp-cron.php. Some server configurations prevent scripts sending a request to the same domain … Read more

Automated mark posts as featured every day

OK, so first of all you’ll need to add your own WP_Schedule event: add_action( ‘wp’, function () { if (! wp_next_scheduled ( ‘mark_posts_as_featured_event’ )) { wp_schedule_event(time(), ‘daily’, ‘mark_posts_as_featured_event’); } } ); function mark_posts_as_featured_event_callback() { // if there are sticky posts in our CPT, unstick them $sticked_post_ids = get_option( ‘sticky_posts’ ); if ( ! empty ) … Read more