JohnSnow
JohnSnow

Reputation: 7121

Display custom post types of a certain category only

I have some faqs on my site and want to show only faqs under a certain category. The categories include worker, company, test.

Here is my code:

$faq = new WP_Query(array(
          'post_type'=>'faq',
          'order' => 'DESC',
          'category_name' => 'test',
        ));
  while($faq->have_posts()) : $faq->the_post();

this should be showing me all the posts under the category of test but it is not doing that. I have also tried cat => (id) and still no result. The loop is always empty.

Any ideas?

Upvotes: 0

Views: 585

Answers (1)

Alejandro Sanchez
Alejandro Sanchez

Reputation: 76

This question has already been answered here and here but I will summarise the answers applied to your particular case.

The category_name parameter needs to be the slug of the category, and not the category name, you are using:

'category_name' => 'test'

Does one of your categories have the slug "test"?

Here are other parameters you can use instead:

cat (int) - use category id.
category_name (string)- use category slug (NOT name).
category__and (array) - use category id.
category__in (array) - use category id.
category__not_in (array) - use category id.

Check your results like this:

$faq = new WP_Query(...);    
print_r($faq->posts); die();

That will display the list of posts on the screen (for debugging purposes).

Upvotes: 1

Related Questions