5

I am new to wordpress. My site had been posting scheduled posts, but it seems to stopped due to no traffic. So I disabled wp default cron, and setup a cron job via cPanel.

via command wget -q -O - http://1pm.today/wp-cron.php?doing_wp_cron

And usr/bin/curl --user-agent cPanel-Cron http://1pm.today/wp-cron.php?doing_wp_cron

But running wp-cron.php like this doesn't seem to trigger scheduled post.

I even tried to trigger cron job from a browser (not sure if it is the right way) http://1pm.today/wp-cron.php?doing_wp_cron

But none of this trigger publish any scheduled post. Should cron job like this suppose to publish scheduled post? If so, any advice to debug this would be greatly appreciated, I will try to work with the tech support as the site is on a shared host.

I have added echo statement in wp-cron.php

function _get_cron_lock() {
    global $wpdb;

    $value = 0;
    if ( wp_using_ext_object_cache() ) {
        /*
         * Skip local cache and force re-fetch of doing_cron transient
         * in case another process updated the cache.
         */
        $value = wp_cache_get( 'doing_cron', 'transient', true );
    } else {
        $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", '_transient_doing_cron' ) );
        if ( is_object( $row ) )
            $value = $row->option_value;
    }

    return $value;
}

if ( false === $crons = _get_cron_array() )
    die();

$keys = array_keys( $crons );
$gmt_time = microtime( true );
echo "$keys[0] > $gmt_time";
if ( isset($keys[0]) && $keys[0] > $gmt_time )
    die();

echo ":6c";
// The cron lock: a unix timestamp from when the cron was spawned.
$doing_cron_transient = get_transient( 'doing_cron' );
echo "7";

And the output is

1465529020 > 1465525264.31 without reaching ":6c"

So it die()s because of

if ( isset($keys[0]) && $keys[0] > $gmt_time )

1465529020 > 1465525264.31

May I ask where $keys[0] came from?

I am guessing the $key[0] is a timestamp of the first cron job and $gmt_time is the current time, and if $key[0] is in the future and it should abort.

I already have a lot of "miss scheduled" post, so I am trying to better understand _get_cron_array() but I don't get what's going on in `wp-includes/cron.php.

It seems that _get_cron_array() has something to do with cron in wp_options

SELECT *
FROM `wp_options`
WHERE `option_name` LIKE '%cron%'

At this point, I am even more confused, as I can't figure out how a scheduled post is linked to the cron job.

1
  • 2
    Have you checked your server time (cli) and you actual application time?
    – kaiser
    Jun 15, 2016 at 10:29

1 Answer 1

1

The _get_cron_array() function gets the 'cron' option out of the database. The cron option contains an array consisting of a series of unix-timestamps and action hooks.

So, when the timestamp is reached, then the action hook is called.

The timestamp of 1465529020 is the equivalent of June 10th, 2016, at 3:23am in UTC time.

The timestamp of 1465525264 is the equivalent of June 10th, 2016, at 2:21am in UTC time.

So, basically, that action was scheduled to run 1 hour and 2 minutes later. Which is why wp-cron didn't run it. It wasn't time yet.

Scheduling a post to publish at a specific time creates an entry in the cron array with the time to publish it as the key, and the action that it fires to publish it as the value. When the time is reached, the action hook gets fired, and the post gets published. That's how wp-cron.php works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.