简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM