creating wp query with posts with specific category

Im running a specific wp_query to show the thumbnails in a slider of a specific category.

<?php $the_query = new WP_Query ('showposts=2', 'category_name=Events'); ?>

This doesnt appear to be working, am i missing array at the beginning somewhere

<?php $the_query = new WP_Query array('showposts=2', 'category_name=Events'); ?>

the above seemed to crash it.

Any advice would be great.

1 Answer
1

Use either:

$the_query = new WP_Query('posts_per_page=2&category_name=events');

or

$the_query = new WP_Query(array(
    'posts_per_page' => 2,
    'category_name' => 'events', // this is the category SLUG
));

// EDIT
Please note that category_name actually is the category slug (which is for one-word names originally the same). It is not the actual name (maybe having spaces, special characters etc. in it), though. In your case, where you just want to specifiy a certain category, you can do this either by means of the ID (cat=42) or the slug (category_name=events).

If you really want/have to use the name, you have to use get_cat_ID('Category Name'), for instance like so:

$the_query = new WP_Query(array(
    'posts_per_page' => 2,
    'cat' => get_cat_ID('Events'),
));

Leave a Comment