Skip Navigation

[Resolved] child categories view

This support ticket is created 4 years, 1 month 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 8 replies, has 2 voices.

Last updated by jelle-jacobd 4 years ago.

Assisted by: Jamal.

Author
Posts
#1823799

I would like some advise to setup a view which shows only child taxonomies. In my case Product categories. At this point I have setup a view which loops trough all the categories

#1826267

Hello Jelle,

I come up with the following shortcode that will get only the terms that are not parents of other terms for the same post:

add_shortcode('jts_last_terms', 'jts_last_terms_func');

function jts_last_terms_func($atts){
	$atts = shortcode_atts(
		array(
			'taxonomy' => 'product_cat',
			'post' => '',
		),
		$atts
	);

	$terms = get_terms($atts['taxonomy'],$atts['post']);

	// error_log(print_r($terms, true));

	$parents = array();

	foreach ($terms as $term) {
		if ( $term->parent > 0 ) $parents[] = $term->parent;
	}

	// error_log(print_r($parents, true));

	$lasts = array();
	foreach ($terms as $term) {
		if( !in_array($term->term_id, $parents) ) $lasts[] = $term->term_id;
	}

	return implode(',', $lasts);
}

The shortcode expects two arguments, (post, and taxonomy). And you will have to change the query filter on the view to take IDs from a shortcode. Check this screenshot hidden link
You can use it to pass IDs to the view like this:

[wpv-view name="your-taxonom-view-name" terms="[jts_last_terms post='[wpv-post-id]' taxonomy='product_cat']"]

I wanted to test this on your website, but for some reason, the Zephyr content template is not being used for products anymore. So, I'll let you add it by yourself. Let me know if you still need assistance with this.

#1826445

Hi Jamal,

Thanks a lot, Looks good:) Unfortunatly the shortcode for the view isn't working. I guess it has something to do with the '' or "". My page isn't rendering correctly. What should be the correct syntax?

Thanks, Jelle

#1829943

Hello Jelle,

I do not think that it is caused by single and double quotes ('' or ""), I would say that it is caused by the WPBakery Page builder not being able to accept 3 level of shortcodes such as:

[wpv-view name="product-table-child" terms="[jts_last_terms post='[wpv-post-id]' taxonomy='product_cat']"]

But from this product hidden link I think that you are already getting what you need. The information on the last term child. Check this screenshot hidden link

Is there any other product where this does not work?

#1830081

Hi Jamal,

Thanks! Oke, I tried to register the shortcode first in Toolset > Front-end > Third party shortcodes. But this doesn't do the trick either.
hidden link

The product you reviewd is skipping the first result from the view. That's how I implemented it before, and which I restored for the short term. This solution isn't waterproof, as in some cases the parent category is returned as second result.

Br, Jelle

#1831151

Hello Jelle,

Registering the shortcode won't help, it is required when the shortcode is being used inside a conditional shortcode or block.

Because the shortcode argument approach did not work, I tried to hook into the view's results and remove the top-level terms, where the parent=0. I used the following code in Toolset->Settings->Custom Code section, and it seems to work.

add_filter( 'wpv_filter_taxonomy_post_query', 'exclude_top_level_terms_func', 30, 4 );
  
function exclude_top_level_terms_func( $items, $tax_query_settings, $view_settings, $view_id ) {
  if( $view_id != 377706 ) return $items;
  $results = array();
  foreach( $items as $item ) {
    if ( $item->parent != 0 ) $results[] = $item;
  }
  return $results;
}

I hope this helps. Let me know if you have any questions.

#1831273

Hi Jamal,

Thanks, I removed the shortcode again.

Looks good, but the views settings stil are filtering the first record correct? According to Limit and startpoint settings of the view? Isn't this doing kinda the same? If I set these settings back to normal, the view returns parent and child again, so it's not clear to me what the hook is doing?

Br. Jelle

#1831709

Hello Jelle,

I was aware of the limit and offset in the configuration of the view, and I think that I have removed it, but I did not talk about it in my last reply.

The view will query the terms of the current post(both parent and child) and this hook will loop over the results and remove the parent term that has a parent=0. Check this screenshot from the view's debug information, it already returns 2 terms, then we remove the parent. Check this screenshot hidden link

I updated the view again to remove the limit and offset configuration. Check this screenshot hidden link

Best regards,
Jamal

#1832075

Hi Jamal,

Yes, It works. Great:) Thank you for taking the time and effort to offer a working solution. Much appreciated. Did you customize the code or just removed the query filter options? I tried this myself yesterday, but that didn't seem to work.

Br, Jelle