• Hi,

    Post Not Insert into the database on Cron Job.Cron executing but post could not be inserted to database Below my code

    function api_call()
    	{
    $post = array(
    				    'post_author' => 1,
    				    'post_content' => $productDetails->description,
    				    'post_status' => $productDetails->status,
    				    'post_title' => $productDetails->title,
    				    'post_parent' => $productDetails->parent_id,
    				    'post_type' => "product",
    					);
    $post_id = wp_insert_post( $post, $wp_error );
    }
    add_action( 'init', 'register_daily_revision_delete_event');
    function register_daily_revision_delete_event() {
    	
    	wp_schedule_event( time(), 'daily', 'api_call' );
    	
    	}
    
    add_filter( 'cron_schedules', 'add_custom_cron_intervals', 10, 1 );
    
    	function add_custom_cron_intervals( $schedules ) {
    		// $schedules stores all recurrence schedules within WordPress
    		$schedules['ten_minutes'] = array(
    			'interval'	=> 300,	// Number of seconds, 600 in 10 minutes
    			'display'	=> 'Once Every 5 Minutes'
    		);
    
    		// Return our newly added schedule to be merged into the others
    		return (array)$schedules; 
    	}
    • This topic was modified 7 years, 10 months ago by Rupam Hazra.
    • This topic was modified 7 years, 10 months ago by Rupam Hazra.
    • This topic was modified 7 years, 10 months ago by bcworkz. Reason: code format fixed
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Hello Rupam,

    To hopefully avoid potential confusion, WP scheduled events are not in any way related to server Cron Jobs. WP events are triggered by site visitation, not a server’s CronTab entry. If you have very low visitation, a true server Cron job could be used to trigger events, but this is usually unnecessary.

    I see a few problems. One is you must not schedule events from the ‘init’ action. This would add a new set of recurring events every time a request is made. You could end up with many hundreds accumulated in only a few hours! The schedule event call needs to be configured to only run once. Until the event is removed, it will keep recurring endlessly despite being called only once. This is often done by tying the function call into the plugin or theme activation. The event is then removed upon deactivation. You should call wp_clear_scheduled_hook('api_call') one time to clear out currently accumulated events.

    The hook name you pass to wp_schedule_event() (‘api_call’) is not a function name. It is an action hook tag. Thus, to have your function called when the action hook fires, you must add your function to that action. Please refer to the doc’s example to see how to set this up.

    Where is the post data coming from? In your api_call() function, $productDetails is undefined. You will be getting Undefined variable warnings and empty posts as it stands.

    There are some other discrepancies, but they are unrelated to posts not being inserted. You’ve added a ten minute interval, but your schedule interval is daily. The ten minute interval is set for 600 seconds, but the description says ‘Every 5 Minutes’

    When you post code, please use backticks or the code button. Failing to correctly format code makes it difficult for other members to test snippets because all the quote characters are wrong.

    Moderator bcworkz

    (@bcworkz)

    @rupamhazra — it does not look like you are subscribed to your own topic, so you will not be notified when replies are posted unless you are @ referenced as I did here. If this is intentional, then all is well. Otherwise you should visit your topic and subscribe by using the button in the right sidebar.

    Thread Starter Rupam Hazra

    (@rupamhazra)

    @bcworkz hi first of all api_call is my function its fired whenever crons executed and then how i executed a cron on wordpress.Please help me

    Moderator bcworkz

    (@bcworkz)

    I explained what to do in my initial post. Is there something I need to clarify for you?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Post Not Insert into database on Cron Job’ is closed to new replies.