Skip Navigation

[Resolved] how to show parent category post in child category archive?

This support ticket is created 5 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by Himanshu Agarwal 5 years, 9 months ago.

Assisted by: Nigel.

Author
Posts
#1190427

I created archive for product category, I need to show parent category products in a child category archive display.
For details lets assume

All
->business
--> Work from home
--> online business

Current if we open the archive for "All" then all post form child category (business, Work from home, online business ) will show and if we open the archive for "Business" then all post form child category (Work from home, online business) will show.
And this default functionality of wordpress and toolset.

But I need that if archive for "online business" (child category) opened then also show post from all parent categories (All,Business) of this category.

Please help me to achieve this.

#1190477

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

WordPress archives work the way that WordPress archives work, which is to generate a list of posts determined by the URL, e.g. posts with the term of a particular taxonomy.

You can customise this using Views inasmuch as you can apply additional filters to the main query, e.g. only include posts with a certain custom field value.

But this will always reduce the set of results. It is not possible to expand on the results when using custom archives.

To do that you would need to add some custom code, and you would be using the WordPress API to directly modify the queries, Toolset doesn't have a role in this.

You would need to use pre_get_posts to intercept the query, the check that you were on the right page (i.e. the taxonomy archive page for this taxonomy) using the template tag is_tax() and that this query is the main query on the page (using $query->is_main_query()), and then modify the query vars to include not just the current term but also the full hierarchy.

See
https://developer.wordpress.org/reference/hooks/pre_get_posts/
https://codex.wordpress.org/Function_Reference/is_tax

If you are not able to code this yourself you'll need to recruit a developer to do it for you. (They wouldn't need to be a Toolset contractor, any WordPress developer should be able to do it for you.)

#1197162

Thank you!