WordPress.org

Ready to get started?Download WordPress

Forums

Trying to Move a Post from One Category to Another (2 posts)

  1. phoenixfireball
    Member
    Posted 4 years ago #

    Issue:

    Post will not update categories when the wp_update_post function is being called from wp_cron, but it will if you call it from within a template file. (ie. calling the function from single.php)

    This function works (it’s placed in the functions.php and called from the single.php)

    function update_it($post_id)
    {
    $new_cats = array(71,4);
    wp_update_post(array('ID'=>$post_id,'post_category'=>$new_cats,'post_status' => 'published'));
    }

    This is the modified code from the Post Expirator plugin, when it is called, it updates the post_status, but not the categories.

    function expirationdate_delete_expired_posts() {
    global $wpdb;
    $result = $wpdb->get_results('select post_id, meta_value from ' . $wpdb->postmeta . ' as postmeta, '.$wpdb->posts.' as posts where postmeta.post_id = posts.ID AND posts.post_status = "publish" AND postmeta.meta_key = "expiration-date" AND postmeta.meta_value <= "' . mktime() . '"');
    if (!empty($result)) foreach ($result as $a) {
    $post_result = $wpdb->get_var('select post_type from ' . $wpdb->posts .' where ID = '. $a->post_id);
    if ($post_result == 'post') {
    $expiredStatus = strtolower(get_option('expirationdateExpiredPostStatus'));
    } else if ($post_result == 'page') {
    $expiredStatus = strtolower(get_option('expirationdateExpiredPageStatus'));
    } else {
    $expiredStatus = 'draft';
    }

    if ($expiredStatus == 'delete')
    wp_delete_post($a->post_id);
    else {
    $new_cats = array(71,4);
    wp_update_post(array('ID'=>$a->post_id,'post_category'=>$new_cats,'post_status' => 'draft'));

    delete_post_meta($a->post_id, 'expiration-date');
    update_post_meta($a->post_id, 'expiration-date', $a->meta_value, true);
    }
    }
    }
    add_action ('expirationdate_delete_'.$current_blog->blog_id, 'expirationdate_delete_expired_posts');

    Link to plugin: http://wordpress.org/extend/plugins/post-expirator/

  2. robanna
    Member
    Posted 4 years ago #

    Did you ever have any luck using wp_update_post?
    I would much rather have the category changed than have it set to draft.
    The next version of PostExpirator may have this functionality but I can't wait. I'm already too far down that road.

    Thanks-
    Rob

Topic Closed

This topic has been closed to new replies.

About this Topic