Support » Plugins » Hacks » Show only post from selected category

  • Hi.

    I have searched for about 10 hours now and even though I’ve found some category template hacks out there, the ones that kind of work still don’t fix the problem.

    The same problem/behaviour goes for standard posts as for Woocommerce product listings: when a base category is selected, WP shows all posts, even those from subcategories.

    Even if I somehow manage to code a template that would list the desired products, the product counter would still be wrong because that’s part of a different php file that’s loaded before the template.

    So I am looking for a hack/filter that would alter the core WP code so that when I list posts from a certain category, none from subcategories are returned. This is the behaviour I want for all posts on the site.

    Thanks for reading.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Can’t you just remove the products from the top level category? Does that not work? What do you gain by having them in the top level category but not showing up when it is shown?

    I am making a copy of an existing webshop using a new graphical design.

    My job is the coding – they tell me what they want, and I make it happen. The slightest difference makes the others.. depressed. As a programmer, I know this is totally doable, probably just by adding, removing or editing a row in one of the files, but I have no idea or time to look through possibly thousands of them, so I would appreciate the help.

    Moderator bcworkz

    (@bcworkz)

    Hook the ‘pre_get_posts’ action and set the ‘tax_query’ query var to an appropriate tax query array that includes 'include_children' => false. To query for all categories except children, try an empty terms array and 'operator' => 'NOT IN'. I’m not sure how that will work with a no children argument, see what happens. If that doesn’t work, there’s surely something along the same line that will work. Good luck!

    Thanks a lot for the replies. I really appreciate them and will try out bcworkz’s idea when I have time.

    Fortunately, this won’t be an issue (in the store) anymore since we’ll be converting all parent categories links to texts..

    I already had a “fix” for this same issue with normal posts and for now, it seems to work, although I have no idea if it will break anything if the post count exceeds a certain number.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show only post from selected category’ is closed to new replies.