Chris
Chris

Reputation: 313

Custom Post Type - Get posts by category

I am using the following code to return all posts from my custom post type "portfolio".

<?php global $wp_query;

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

$args = array('post_type' => 'portfolio', 'posts_per_page' => 3, 'paged' => $paged);

$wp_query = new WP_Query($args); ?> ...

On other pages i would like to return posts by a category of my custom post type, of which i have created a taxonomy for. I am using the following code below but still returning all custom posts.

<?php global $wp_query;

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

$args = array('post_type' => 'portfolio', 'taxonomy' => 'portfolio_categories', 'category' => '5', 'posts_per_page' => 3, 'paged' => $paged);

$wp_query = new WP_Query($args); ?>

Any help or direction would be appreciated.

Upvotes: 0

Views: 1328

Answers (1)

Jackson
Jackson

Reputation: 264

You should use tax_query if you want to list the posts under a category:

 $args = array(
        'posts_per_page'  =>  3,
        'tax_query' => array(
        array(
                'taxonomy' => 'portfolio_categories',
                'terms' => '5'
            ),
         ),
        'post_type'        => 'portfolio',
        'post_status'      => 'publish'
        );

Upvotes: 1

Related Questions