0
votes

Hi I have some problem with custom post type categories, I try to get all post that have some category but it output all post. Can you help me please. Thank you.

my code

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'post_type' => "collection-posts",
    'category'=> 1,
    'posts_per_page' => 12,
    'paged' => $paged
);
query_posts($args);

but it give all posts. I don't understand why

2

2 Answers

2
votes

The arguments key should be cat, not category:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'post_type' => "collection-posts",
    'cat' => 1,
    'posts_per_page' => 12,
    'paged' => $paged
);
query_posts($args);
0
votes

I found another solutions. You can also use get get_posts method of WordPress and category slug. For Example : i am assuming your category slug is collection-posts-category

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
     'category'  => 'collection-posts-category',
     'post_type' => 'collection-posts',
     'posts_per_page' => 12,
     'paged' => $paged
);
$collection_posts = get_posts($args);

Hope this will helpful.