While writing my Bulk Move WordPress Plugin, I faced a unique bug. I was breaking my head for nearly two days to find out the correct way to retrieve the categories of a blog post in WordPress. I thought of documenting it here so that I know where I can search for it in future, when I need it and also it might help other WordPress developers who are hunted by the same bug.
Let me try to explain what I was trying to do, why it failed and finally how I rectified it.
Well I was trying to retrieve all the blog posts which satisfy a certain condition and then loop through them and assign them to a new category.
I was using the following code to retrieve the blog posts which satisfied a certain condition. Note that that I am using the built-in WordPress API as much as possible and not directly querying the database. This is one of the good coding practices for writing WordPress Plugins.
$my_query = new WP_Query;
$posts = $my_query->query(array('category__in'=>array($cat_id1, $cat_id2), 'post_type'=>'post', 'nopaging'=>'true'));
Now the variable $posts will have an array of post objects. You can loop through them using foreach.
foreach ($posts as $post) {
print_r($post);
}
Will print
Array
(
[ID] => 33
[post_author] => 1
[post_date] => 2009-01-19 08:32:41
[post_date_gmt] => 2009-01-19 08:32:41
[post_content] => Test from Sudar...
[post_title] => Sudar Test
[post_category] => 0
[post_excerpt] =>
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => sudar-test
[to_ping] =>
[pinged] =>
[post_modified] => 2009-02-04 17:54:10
[post_modified_gmt] => 2009-02-04 17:54:10
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://sudarmuthu.com/?p=33
[menu_order] => 0
[post_type] => post
[post_mime_type] =>
[comment_count] => 0
)
If you look into the post object, you will see a column called post_category
. I was trying to retrieve the category/categories of the blog post by reading this variable. But to my horror, I was getting only zero as value in this variable.
After some hours of hair pulling, I posted about this in the WP-hackers mailing list. It was only then I came to know that post_category
is deprecated and we have to use other functions to retrieve the categories to which a blog post belongs to.
So the correct way to retrieve the category/categories is
foreach ($posts as $post) {
$post_cats = wp_get_post_categories($post->ID);
}
$post_cats
will be an array of category ids to which the blog post belongs.
So this is the complete code which you use to retrieve the list of categories to which a blog post belongs.
I didnโt know the fact that the post_category was deprecated and I learned it the hard way ๐
Even though the post_category
field is deprecated it is not entirely useless. It can be used while updating a blog post.
Letโs say that you want to move a blog post from one category to another. In this case you can set the array containing the category ids to post_category
field and pass it to the wp_update_post ()
function. Code snippet below
foreach ($posts as $post) {
$new_cats = array(5,10);
wp_update_post(array('ID'=>$post->ID,'post_category'=>$new_cats));
}
Whew!! Finally I know how the correct way to retrieve the categories to which a blog post belong to. ๐