Question

Is it possible to move a post to an another category if post_meta field is 0 ?
I'm building a reverse order vote, every post have by default 10 votes and after a user vote, removes one vote by updating the post_meta, now i try to move the post to an another category if the votes field is 0 or 2 days old.

I have found a solution here that moves a post to an another category if is a past date from this in the database.
It runs every hour to do the check

if (!wp_next_scheduled('sfn_expire_hook')){
    wp_schedule_event( time(), 'hourly', 'sfn_expire_hook');
}

add_action( 'sfn_expire_hook', 'sfn_show_expire');
function sfn_show_expire(){
    global $wpdb;
    $server_time = date('mdy');
    $result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'");
    if( !empty($result)) foreach ($result as $a){
        $show_time = get_the_time('mdy', $a->ID);
        if ( $server_time > $show_time ){
           $my_post = array();
           $my_post['ID'] = $a->ID;
           $my_post['post_status'] = 'draft';
           wp_update_post( $my_post );
        }
    } // end foreach
}

can someone help me to extend this to check for 2 days old posts and if post_meta "votes" is "0" also?

thanks a lot,
Philip

Was it helpful?

Solution

When you remove vote check for your condition, then if needed:

  1. fetch categories with wp_get_object_terms() (low level func, you can specify to fetch names or ids only since you don't need full objects for this)
  2. modify array (remove category you don't need, add the one you do)
  3. set categories with wp_set_post_categories() (it will overwrite all categories so those you remove from array will be gone)
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top