Call WordPress category by name instead of ID

I’m working on a local copy of a WP site, but when we move it live all of the category IDs will change, so I need to find a way to call things by name instead of ID. Right now I have this:

$myposts = get_posts('numberposts=3&category=3599');

I actually need to show posts from two categories, so instead of the category being 3599, I need it to be Technology and Technology News. Is there a way to do this?

Related posts

Leave a Reply

3 comments

  1. get_posts can use all parameters that the WP_Query can, so try this:

    $myposts = get_posts('numberposts=3&category_name=[Technology],[Technology News]');
    

    Replace [Technology] and [Technology News] with the slug name of the two respective categories (and remove the brackets, of course). Make sure you use the slug name and not the regular name or it won’t work.

    Also, please make sure to test this first. I ran a brief test on a local installation of WordPress 3.5.0, but I don’t think I have to emphasize to you that you should make sure it works as expected on your end!

    Good luck.

  2. Figured it out – I just needed to change ‘category’ to ‘category_name’

    $myposts = get_posts('numberposts=3&category_name=technology,technology-news');