Aram Mkrtchyan
Aram Mkrtchyan

Reputation: 2700

Wordpress get custom post type posts by category

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

Upvotes: 0

Views: 3505

Answers (2)

Amit Kumar PRO
Amit Kumar PRO

Reputation: 1240

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.

Upvotes: 0

George
George

Reputation: 36794

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);

Upvotes: 2

Related Questions