I have a hieratical category structure using Taxonomies, with a custom post displayed within them.
Currently the higher level categories display all the posts that are in their child categories. How do I make it only list from the current category only and not their children.
I'm currently using the default code.
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
I'm not a programmer and have not been ably to find anything with an exact or close enough match for me to work it out.
I have a hieratical category structure using Taxonomies, with a custom post displayed within them.
Currently the higher level categories display all the posts that are in their child categories. How do I make it only list from the current category only and not their children.
I'm currently using the default code.
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
I'm not a programmer and have not been ably to find anything with an exact or close enough match for me to work it out.
Share Improve this question asked Nov 3, 2014 at 12:32 Legin76Legin76 1171 silver badge7 bronze badges 14- possible duplicate of How to only show posts on last child category – Pieter Goosen Commented Nov 3, 2014 at 18:20
- I'm not sure it is a duplicate but is similar.. The solution ciprianmocanu gave is easier to implement or at least understand. – Legin76 Commented Nov 4, 2014 at 12:02
- You should never change the main query for a custom one. The 2nd solution by @ialocin is the correct way to achieve this. And that solution is real easy – Pieter Goosen Commented Nov 4, 2014 at 12:27
- Where should I put it? I've tried both before and after if (have_posts()) : while (have_posts()) : the_post(); with no luck. – Legin76 Commented Nov 4, 2014 at 14:48
- Add it in functions.php – Pieter Goosen Commented Nov 4, 2014 at 14:49
1 Answer
Reset to default 4Did you try WP_Query? Using my knowledge and the WP_Query documentation, I did this:
$args = array(
'post_type' => 'my_post_type', //change the post type here
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'my_category', //change the taxonomy name here
'field' => 'id',
'include_children' => false,
'terms' => 10 //change the term id here
)
)
);
$_query = new WP_Query($args);
if ($_query->have_posts()):
while ($_query->have_posts()):
$_query->the_post();
//do something here the_title() etc
endwhile;
endif;
wp_reset_query();
The magic should come from the include_children
attribute. Test it out :)
Is this what you wanted?