lundi 2 février 2015

Addings Existing Categories to Posts


I have a lot of existing posts +10000 which don't have a category. I tried the bulk edit feature of wordpress. However i get a 504 above 100 items. So that will take a very long time.


I would like know how i can change all posts into 1 category in a easy way.


I tried several codes so far. Like; ?php $args = array( 'posts_per_page' => -1, 'orderby' => 'post_date' ); $posts = get_posts( $args ); foreach ( $posts as $post ) : setup_postdata( $post ); $category_id = 67; if ( !(in_category( $category_id )) ) { wp_set_object_terms( get_the_ID(), array( $category_id ), 'category', false ); } endforeach; ?>


The above code did not work at all, offcourse i changed the category id in my existing code. And i also tried to add this code in the header.php - footer.php and functions.php


Then i tried the following code;


// An array of IDs of categories we to add to this post. $cat_ids = array( 6, 8 );


/* * If this was coming from the database or another source, we would need to make sure * these where integers:


$cat_ids = array_map( 'intval', $cat_ids ); $cat_ids = array_unique( $cat_ids );


*/


// Add these categories, note the last argument is true. $term_taxonomy_ids = wp_set_object_terms( 42, $cat_ids, 'category', true );


if ( is_wp_error( $term_taxonomy_ids ) ) { // There was an error somewhere and the terms couldn't be set. } else { // Success! These categories were added to the post. }


In the above code i changed my category id and the post id. This worked, however it works only for 1 post. I need to change all posts. So i added more posts id's like; $term_taxonomy_ids = wp_set_object_terms( 42, 43, 44 $cat_ids, 'category', true );


However this did also not work.


I would be very very glad if someone can help me out.





Aucun commentaire:

Enregistrer un commentaire