Shibbir
Shibbir

Reputation: 2041

How to show all custom post from custom taxonomy?

I have a custom post called item and custom taxonomy under this custom post called item_category

Now, I want to get all posts based on this custom taxonomy.

To do that, I have this URL of custom taxonomy:

http://localhost/mysite/item_category/event/

Here, item_cateogry is the custom taxonomy and event is the term of this taxonomy.

So, my code is below but not showing anything.

$queried_object = get_queried_object();
$current_category_name = $queried_object->slug; // will print the `event`

$items = new WP_Query(array(
    'post_type' => 'item',    
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => $current_category_name,
            'field'    => 'slug',
        ),
    ),
));

Upvotes: 0

Views: 26

Answers (1)

Shibbir
Shibbir

Reputation: 2041

I have found the issues:

Here is the right code:

$items = new WP_Query(array(
    'post_type' => 'item',    
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'item_category',
            'field' => 'slug',
            'terms' =>  $current_category_name
        ),
    )
));

Upvotes: 1

Related Questions