Molly Campbell
Molly Campbell

Reputation: 439

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?

Upvotes: 2

Views: 1304

Answers (3)

Molly Campbell
Molly Campbell

Reputation: 439

Figured it out - I just needed to change 'category' to 'category_name'

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

Upvotes: 0

Juan Rangel
Juan Rangel

Reputation: 1793

You should be able to call the name in an array like so. $myposts = get_posts( 'name' => 'News', );

http://codex.wordpress.org/Function_Reference/get_the_category

Upvotes: 1

raphaelvalerio
raphaelvalerio

Reputation: 316

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.

Upvotes: 0

Related Questions