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
评论列表(0条)