Thread Starter
yortem
(@yortem)
thats not what i mean, not the url, the page archive.php
if you are in Archive called “Food”, you will see all the posts that are in Food, Yellow Food, Bananas, Lemons, Red Food, Green Food…
i dont want thatm i want to display only those posts that are directly under Food.
if there are posts under sub category they wont show.
-
This reply was modified 6 years, 5 months ago by
yortem.
I’ve been googling this for a while with no easy way to do that.
I agree with Steve because it sounds like you are conflating post hierarchies (well page or custom post type hierarchies, posts are not hierarchical) and category or taxonomy hierarchies. You can get immediate child pages of a parent without further descendants. You can get immediate child categories of a parent without further descendants. You cannot combine the two at the same time, they are different sorts of objects.
But once you do have the immediate children, you can get all terms of child pages or all posts of child categories. Doing so will require multiple or compound queries. I don’t know if that’s what you’re after or not, but this is how WP queries work.
Thread Starter
yortem
(@yortem)
it has nothing to do with child posts or child pages.
its regular posts that are inside child category.
what im asking is not something unusual, i just want to see the current category’s posts, thats all. i dont want to see the sub-categories posts.
maybe i really dont understand.
-
This reply was modified 6 years, 5 months ago by
yortem.
Thread Starter
yortem
(@yortem)
my solution was very simple. i replaced the query in the archive with a new one that has “‘category__in’ => $current_cat,” in it.
like that
$archive_args = array(
post_type => 'products', // get only products
'category__in' => $current_cat,
);
$archive_query = new WP_Query( $archive_args );
and changed the while line
while ( $archive_query->have_posts() : ) : $archive_query->the_post();
and it worked. the code is correct?
If that’s delivering the results you desire and not generating any PHP warnings, then yes, it’s correct. I needed to qualify my answer because it’s difficult to assess code out of context. What I can see is correct, but there could be issues due to what I cannot see.
Apologies for our misunderstanding of what you were after. I’m glad you were able to find a solution.