We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47212
    • 40 Posts
    Hello,

    I was surprised to see that we need to manually clear MODX cache in order to see the publications status updated if we use pub_date and unpub_date fields.
    I write a little plugin that handle this and I would like to put it there for those who want this behavior too.

    What the script does :
    If pub_date or unpub_date are set, we check if the date/time is reached, if so, we clear the cache part related, then we reload the current resource.

    Create a plugin "ClearCacheOnPubDate" that fires on the OnDocFormPrerender event :
    <?php
    if ($mode === 'upd') {
        $refreshNeeded = false;
    
        foreach(array('pub_date', 'unpub_date') as $field) {
            if ($resource->get($field)) {
                if (strtotime($resource->get($field)) < time()) { $refreshNeeded = true; }
            }
        }
    
        if ($refreshNeeded) {
            $modx->cacheManager->refresh(array(
              'auto_publish' => array('contexts' => $resource->get('context_key'))
            ));
            $modx->sendRedirect(MODX_MANAGER_URL . '?a=resource/update&parent=' . $resource->get('parent') . '&id=' . $id);
        }
    }
    
    unset($refreshNeeded);


    Hope this will help.