How to change post categories with a script.
-
I would like to change post categories with a script run regularly via cron. The script would:
- Gather the posts in category X,
- gather the highest rated post from that group (based on the results of a plugin), and
- then remove category X from the post gathered in ” step 2″.
Any idea on the best way to accomplish this?
Thanks!
-
It would be similar to this:
<?php $tag_name = 'tag1'; // tag to 'delete' $taxonomy = 'post_tag'; $tag = get_term_by('name',$tag_name, $taxonomy); $objects = get_objects_in_term($tag->term_id,$taxonomy); $pop_post_id = 72; //replace this with whatever the poplular post stuff is: if ($pop_post_id) { $args=''; $old_tags = wp_get_post_terms( $pop_post_id , $taxonomy, $args); $new_tags = array(); if ($old_tags) { foreach ( $old_tags as $old_tag ) { if ($old_tag->term_id != $tag->term_id) { $new_tags[]=$old_tag->name; } } } wp_set_post_terms( $pop_post_id, $new_tags, $taxonomy, $return ); } ?>
Review: https://codex.www.ads-software.com/Category:WP-Cron_Functions
Darn, it was categories, not tags you wanted…will have to rethink that…
Thanks for the suggestion, looking forward to your afterthought.
I was told that in order to use the WP Functions, I only need to include wp-load.php
Just want to make sure that’s correct as well.
Not much different…
<?php $term_name = 'cat1'; //category to delete $taxonomy = 'category'; $term = get_term_by('name',$term_name, $taxonomy); $objects = get_objects_in_term($term->term_id,$taxonomy); $pop_post_id = 72; //replace this with whatever the poplular post stuff if ($pop_post_id) { $args=''; $old_terms = wp_get_post_terms( $pop_post_id , $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( $pop_post_id, $new_terms, $taxonomy, $return ); } ?>
Remember that WordPress automatically assigns the ‘default’ category if a post does not have a category assigned.
<?php $term_name = 'cat1'; //category to delete $taxonomy = 'category'; $term = get_term_by('name',$term_name, $taxonomy); $objects = get_objects_in_term($term->term_id,$taxonomy); $pop_post_id = 72; //replace this with whatever the poplular post stuff if ($pop_post_id) { $args=''; $old_terms = wp_get_post_terms( $pop_post_id , $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( $pop_post_id, $new_terms, $taxonomy, $return ); } ?>
This will take a category & a post ID and remove the category. Thanks for that! That’s step 3
How about step 1, returning an array of posts based on category X?
Thanks!
EDIT: or does the line “$objects = get_objects_in_term($term->term_id,$taxonomy);” return the array in $objects?
Step 1 is
$term_name = 'cat1'; //category to delete $taxonomy = 'category'; $term = get_term_by('name',$term_name, $taxonomy); $objects = get_objects_in_term($term->term_id,$taxonomy);
$term_name = 'Upcoming'; //category to delete $taxonomy = 'category'; $term = get_term_by('name',$term_name, $taxonomy); $objects = get_objects_in_term($term->term_id,$taxonomy); print count($objects);
This is returning 422. However, I only have 72 posts in the “Upcoming” category.
Any thoughts?
Weird–will have to check that out.
So now
<?php $term_name = 'cat1'; //category to delete $taxonomy = 'category'; $term = get_term_by('name',$term_name, $taxonomy); $postids = array(); $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); 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(). print count($postids); // routine to test $postids against popular posts belongs here $pop_post_id = 72; //this is just for testing if ($pop_post_id) { $args=''; $old_terms = wp_get_post_terms( $pop_post_id , $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( $pop_post_id, $new_terms, $taxonomy, $return ); } ?>
Genius. That worked flawlessly. Are you just pulling this from your experience with the core? Or is this documented somewhere?
Finally, I noticed something with,
if ($pop_post_id) { $args=''; $old_terms = wp_get_post_terms( $pop_post_id , $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( $pop_post_id, $new_terms, $taxonomy, $return ); }
How does this know what category I’m deleting? I’m not sure if I’m reading it correctly (probably not) but does this allow for posts that have multiple categories that I want to keep, but only delete the one I’ve defined?
Also, is there some way I can compensate you for the work you’ve done? Perhaps donate to your site, yourself, or charity of your choosing?
Thanks!
Just looked at core to find that…seems wp_set_post_terms does ‘put the correct terms back on the post’ so it does in effect delete your category.
Your thanks is my payment. Good luck.
seems wp_set_post_terms does ‘put the correct terms back on the post’ so it does in effect delete your category.
Right you are. Works wonders.
Just in care you were wondering the site is https://www.peopleofpublictransit.com
Soon I’ll have your hard work up and running!
Thanks again.
//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: https://www.peopleofpublictransit.com\nPost URL: https://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!
Wow, THANKS!
I’ve been looking forever for a way to remove a post from a category. I had a suspicion wp_set_post_terms might reset all the categories for that post, enabling me to exclude the one I didn’t want, but hadn’t had a chance to try it. Appreciate the help!
I think this solution should also work. Still need to test it out:
wp_delete_object_term_relationships( $object_id, $taxonomies ) X-Ref
Will unlink the term from the taxonomy.
https://xref.yoast.com/wpmu-trunk/nav.html?wp-includes/taxonomy.php.html#wp_delete_object_term_relationships
- The topic ‘How to change post categories with a script.’ is closed to new replies.