wp_update_post not getting triggered by hook

I am not able to make make a hook trigger this action from functions.php file. If I put the same code in footer.php, it

I am not able to make make a hook trigger this action from functions.php file. If I put the same code in footer.php, it works, but not in functions. In the end I would like to set up a cron job to execute this once a day, but for a start this one should be ok. What am I missing here – I have tested several hooks, but nothing seems to happen.

add_action('wp_footer','xpl_find_expired_ads_and_unpublish');
function xpl_find_expired_ads_and_unpublish() 
{    
if(!is_admin()) 
{
    $args = array(
        'post_type' => 'annonse',
        'post_status' => 'publish',
        'date_query' => array(
            array(
                'before' => '-14 days',
            ),
        ),
    ); 
    $ads_query = new WP_Query($args);
    $output = '';
    while ( $ads_query->have_posts() ) 
    {
        if ( $ads_query->have_posts() ) 
        {
            $ads_query->the_post();
            $post_id = get_the_id();
            $unpublish_ad = array(
                'post_status' => 'draft',
                'ID'     => $post_id,
            );
            wp_update_post($unpublish_ad, $wp_error);
            $post_update = wp_update_post( $post_id, true );                          
            if (is_wp_error($post_update)) {
                $errors = $post_update->get_error_messages();
                foreach ($errors as $error) {
                    $output .= $error;
                }
            }
            $output .= $post_id . ', ';
        }
    }
    if (!function_exists('write_log')) 
    {
        function write_log($log) {
            if (true === WP_DEBUG) {
                if (is_array($log) || is_object($log)) {
                    error_log(print_r($log, true));
                } else {
                    error_log($log);
                }
            }
        }
    }
    write_log('THIS IS THE START OF MY CUSTOM DEBUG');
    //i can log data like objects
    write_log($output);
}
} 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745629447a4637013.html

相关推荐

  • wp_update_post not getting triggered by hook

    I am not able to make make a hook trigger this action from functions.php file. If I put the same code in footer.php, it

    22天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信