admin管理员组文章数量:1026156
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);
}
}
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);
}
}
本文标签: wpupdatepost not getting triggered by hook
版权声明:本文标题:wp_update_post not getting triggered by hook 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745629446a2160068.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论