//we don't put the script in the root directory of our wordpress install to prevent people from invoking the script via http. But you can put the script anywhere you want, just make sure to include the wp-load.php wordpress file.
include_once('../public_html/peopleofpublictransit.com/wp-load.php');
//category to delete; change if necessary
$term_name = 'Upcoming';
//type of taxonomy to be changed
$taxonomy = 'category';
//essentially allows the script to get the category ID, which we already knew but this is for the future if we want to make a plugin out of it.
$term = get_term_by('name',$term_name, $taxonomy);
$postids = array();
//this is to tell the script to only pull posts that are labeled "publish"
$args=array(
'category__in' => array($term->term_id),
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
//saves all the posts that meet our category and post criteria in an array called $postids
if ( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$postids[]=$my_query->post->ID;
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
//number of posts in the array starting with 0; we need to know this so we can search the GD Star Rating values for the highest one
$num_of_posts = count($postids);
// routine to test $postids against popular posts starts here. we're just zeroing out some values before the routine runs
$vote_value_wip = 0;
$i = 0;
$post_id_to_update = '';
//go through the array of posts ($postids) and check each one's average rating
while ($num_of_posts > $i){
//check DB for the vote values
$post_id = $postids[$i];
$gdstar = $wpdb->get_row("SELECT * FROM popt_gdsr_data_article WHERE post_id = $post_id", ARRAY_A);
//calculate the vote values
//this ads the plus votes for visitors and users and saves them in $total_plus_votes
$total_plus_votes = $gdstar['user_recc_plus'] + $gdstar['visitor_recc_plus'];
//this ads the minus votes for visitors and users and saves them in $total_minus_votes
$total_minus_votes = $gdstar['user_recc_minus'] + $gdstar['visitor_recc_minus'];
//this gives us the GD Star Rating for the post we're checking
$vote_value = $total_plus_votes - $total_minus_votes;
//this checks if the current post has a higher or equal post rating. if this is the first post, then the script just checks the post rating against "0", which is the initial value. if it's not the first value and the current post is higher than the saved post rating, then it updates the post_id_to_update value to the current post so we know that's the one that needs to be on the front page.
if($vote_value >= $vote_value_wip){
$vote_value_wip = $vote_value;
$post_id_to_update = $postids[$i];
}
$i++;
}
//at this point we have one post id saved. that's the post ID we want to promote to the front page. the value is stored in $post_id_to_update
//here's where we remove the category from the post; the if just makes sure we have a value for $post_id_to_update. the code essentially takes the categories from the post, removes the category we set earlier and then replaces the remaining categories.
if ($post_id_to_update) {
$args='';
$old_terms = wp_get_post_terms( $post_id_to_update , $taxonomy, $args);
$new_terms = array();
if ($old_terms) {
foreach ( $old_terms as $old_term ) {
if ($old_term->term_id != $term->term_id) {
$new_terms[]=$old_term->name;
}
}
}
wp_set_post_terms( $post_id_to_update, $new_terms, $taxonomy, $return );
}
//at this point the "upcoming" has been removed from the post, however, depending on the original publish date, it may not show up on the front page, so we need to update the publish date so it shows up there
//update "Published on" date to current timestamp so post appears on front page in slot 1
$my_post = array();
$my_post['ID'] = $post_id_to_update;
$my_post['post_date'] = date("Y-m-d H:i:s");
$my_post['post_date_gmt'] = gmdate("Y-m-d H:i:s");
wp_update_post( $my_post );
//this just flushes the database queries that were saved, no big deal.
$wpdb->flush();
//optional code below
//this is just for the email we're going to send out via crontab
$time = date("Y-m-d H:i:s");
//our host has set up crontab to email any output a script run via cron. If your host does not do this, you can easily replace the following echo statement with a php mail()
echo "Site URL: http://www.peopleofpublictransit.com\nPost URL: http://www.peopleofpublictransit.com/?p=$post_id_to_update\n$time";
Thanks for your help. I have a small issue. After running the script, < br/ > tags are changed to < br > in the posts.
Is there any way to avoid this? Thanks!