Support » Fixing WordPress » show only direct posts of this category

  • [ Moderator note: moved to Fixing WordPress. Please do not use Developing With WordPress for these topics. ]

    lets say i have these categories
    Food
    – Yellow Food
    — Bananas
    — Lemons
    – Red Food
    – Green Food

    and i have few posts in “Bananas”.

    i dont want the posts to be shown in the categories Food or Yellow Food, only in the direct parent of those posts.

    what should i do?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    the URL for that would be http://example.com/category/food/yellow-food/bananas

    If you want to remove the intermediate levels, there are some suggestions here:

    https://www.google.com/search?q=remove+parent+category+from+url+wordpress

    and this two year old plugin

    https://wordpress.org/plugins/wp-no-base-permalink/

    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.
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    I’ve been googling this for a while with no easy way to do that.

    Moderator bcworkz

    (@bcworkz)

    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?

    Moderator bcworkz

    (@bcworkz)

    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.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘show only direct posts of this category’ is closed to new replies.