Only show posts with the current taxonomy chosen
-
I have created a taxonomy (slug: customcategory) and attached it to my custom post type.
My taxonomy is set to allow parent-child relationships.
This is an example of the hierarchy:- CFO Services
- Processes
- Procure to Pay processes
- Order to Cash
- Treasury & Cash Management
I have then created a post and chosen the “Order to Cash” taxonomy as its category.
On my taxonomy page I want to show all the posts associated with the current taxonomy.
I am doing that with the following code:$posts_array = get_posts( array( 'posts_per_page' => -1, 'post_type' => 'customdocument', 'tax_query' => array( array( 'taxonomy' => 'customcategory', 'field' => 'term_id', 'terms' => array( $id ), ) ) ) ); foreach ($posts_array as $item) { echo $item->post_title; }
When I access my taxonomy page (url: /customcategory/cfo-services/processes/order-to-cash) the code above returns all posts associated to this taxonomy – As it should.
But when I access one of the parent taxonomies (for example: /customcategory/cfo-services/) it returns all the posts associated with the child taxonomies.
My goal is to show only the posts where the current taxonomy is chosen.How can I achieve this?
- The topic ‘Only show posts with the current taxonomy chosen’ is closed to new replies.