WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: Post Expirator] Change Category instead of Change Status (13 posts)

  1. robanna
    Member
    Posted 4 years ago #

    I've been trying to edit Post Expirator to have it change the category instead of setting the status to Draft. I've had no luck. The changes I've tried cause a new revision every 1-2 minutes adding thousands of revisions but it never changes the category. Here's what I've tried:
    I changed line 72 from

    wp_update_post(array('ID' => $a->post_id, 'post_status' => 'draft'));

    to

    $my_post = array();
    $my_post['ID'] = ($a->post_id);
    $my_post['post_category'] = array(96);
    wp_update_post( $my_post );

    I also tried:

    $new_cats = array(96);
    wp_update_post(array('ID' => $a->post_id, 'post_category' => $new_cats));

    The only time it works is if I set the post ID like:

    $my_post['ID'] = 9958;

    So, it makes me think I'm close but I'm not writing that line correctly.

    Can anyone help? I'd really appreciate it. I've been banging my head against the wall with this.

    Thanks-
    Rob

    http://wordpress.org/extend/plugins/post-expirator/

  2. robanna
    Member
    Posted 4 years ago #

    delete_post_meta($a->post_id, 'expiration-date');
    $my_post = array();
    $my_post['ID'] = ($a->post_id);
    $my_post['post_category'] = array(96);
    wp_update_post( $my_post );

    // Set status to 'Draft'
    // wp_update_post(array('ID' => $a->post_id, 'post_status' => 'draft'));
    // delete_post_meta($a->post_id, 'expiration-date');
    // update_post_meta($a->post_id, 'expiration-date', $a->meta_value, true);

    This seems to be working. i don;t think it liked deleting the meta after updating the post cat.

  3. eservice
    Member
    Posted 3 years ago #

    Thank you for posting the correct code. It would be nice if 'changing to a category' would be added in a future release. Changing to draft (so that you can manually put in archive category) causes you to have to 'publish' the article again, which makes it confusing for people reading via RSS.

  4. Shaun Scovil
    Member
    Posted 3 years ago #

    For the latest version of Post Expirator, I changed this code:

    if ($expiredStatus == 'delete')
    			wp_delete_post($a->post_id);
    		else {
    			wp_update_post(array('ID' => $a->post_id, 'post_status' => 'draft'));
    	                delete_post_meta($a->post_id, 'expiration-date');
            	        update_post_meta($a->post_id, 'expiration-date', $a->meta_value, true);
    		}

    ...to this:

    if ($expiredStatus == 'delete')
    			wp_delete_post($a->post_id);
    		else {
    			wp_update_post(array('ID' => $a->post_id, 'post_category' => array(47,!8,!37)));
    	                delete_post_meta($a->post_id, 'expiration-date');
            	        update_post_meta($a->post_id, 'expiration-date', $a->meta_value, true);
    		}

    Notice the ! before two of my category IDs? That takes the post out of those categories.

    In my case, I moved expired posts out of the 'Upcoming Events' and 'Featured Events' categories, and into one called 'Past Events'.

  5. robanna
    Member
    Posted 3 years ago #

    How would you write that line to take it out of Any Category and change it to the new category?

  6. flabrea
    Member
    Posted 3 years ago #

    That is exactly what i am looking for. It would be awesome if it could be this easy :) but changing the code to

    if ($expiredStatus == 'delete')
    			wp_delete_post($a->post_id);
    		else {
    			wp_update_post(array('ID' => $a->post_id, 'post_category' => array(47,!8,!37)));
    	                delete_post_meta($a->post_id, 'expiration-date');
            	        update_post_meta($a->post_id, 'expiration-date', $a->meta_value, true);

    doesn't work. (i changed the categories to my id's of course) the post is deleted as the code was never changed ;(

    this really works at your installation? thanks for helping me out!

  7. Shaun Scovil
    Member
    Posted 3 years ago #

    @robanna: That's a good question! You'd have to first write some code that would get the category (or categories) of a post using get_the_category( $id ).

    Here is a link for more information on that function: http://codex.wordpress.org/Function_Reference/get_the_category

    @flabrea: Not sure what you meant by "the post is deleted as the code was never changed"...but the solution I provided above does the trick in Post Expirator v1.4 on my client's site.

    Do you have the plugin set to 'delete' instead of 'save as draft' in the settings menu?

  8. flabrea
    Member
    Posted 3 years ago #

    sorry, i meant that "the post is deleted as "IF" the code was never changed" / forgot the if. i am using 1.4.3 with wp 3.0.3

    hm. any idea?

  9. Aaron Axelsen
    Member
    Posted 3 years ago #

    Just an FYI - this is on my feature list for the plugin. If someone here gets to it before I do, I would welcome a patch to add it in!

    otherwise, just watch the blog and feature tracker!

  10. flabrea
    Member
    Posted 3 years ago #

    can't wait this feature in the next update :) thanks aaron!

  11. mrwordpress
    Member
    Posted 3 years ago #

    Has this been updated yet? I could really use this function on my site.

  12. Aaron Axelsen
    Member
    Posted 3 years ago #

    unfortunately no - primary jobs and paying projects get priority - I'll let you guys know when I get to it!

  13. robanna
    Member
    Posted 3 years ago #

    I've been using this hack. You lose the ability to set to Draft and it replaces that function with changing the category.

    Near line 86 in post-expirator.php Comment out these 3 lines:

    // wp_update_post(array('ID' => $a->post_id, 'post_status' => 'draft'));
    // delete_post_meta($a->post_id, 'expiration-date');
    // update_post_meta($a->post_id, 'expiration-date', $a->meta_value, true);

    and add these five lines:

    delete_post_meta($a->post_id, 'expiration-date');
    $my_post = array();
    $my_post['ID'] = ($a->post_id);
    $my_post['post_category'] = array(96);
    wp_update_post( $my_post );

    Change 'array(96)' to the ID of the category you wish to have it changed to. All other categories will be removed.

Topic Closed

This topic has been closed to new replies.

About this Topic