Skip Navigation

[Resolved] Get terms from children posts

This thread is resolved. Here is a description of the problem and solution.

Problem:

I would need to display most popular terms for all children posts of a parent post type.

Solution:

It needs custom codes, for example:

https://toolset.com/forums/topic/get-terms-from-children-posts/#post-1690215

Relevant Documentation:

This support ticket is created 4 years, 5 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.

Our next available supporter will start replying to tickets in about 0.38 hours from now. Thank you for your understanding.

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: Asia/Hong_Kong (GMT+08:00)

This topic contains 27 replies, has 3 voices.

Last updated by romanB-3 4 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#1676879

Hi,
I would need to display most popular terms forin all children posts for a parent post type.
CPT1: School
CPT2: Formation
Taxonomy: Domaine
Placed on single School custom template, the view should render list of its Formations' Domaines, ordered by descending number of Formations tagged with each Domaine.
For instance :

"Marketing (32 formations), Management (11 formations), Industry (7 formations)"

What would be the easiest way to get this done ?
The only way I found out was with 4 nested views, but it's really heavy and buggy.
Thank you.

#1677697

Hi,

Thank you for contacting us and I'd be happy to assist.

Your understanding is correct and for what you're trying to achieve, you'll need to use nested views.

I'll be happy to perform some tests on my website with a similar set up to suggest the most efficient way for this.

As soon as this testing completes, I'll update you with my findings.

regards,
Waqar

#1680751

Hi,

Thank you for waiting, while I performed some tests on my website.

Here are the steps that I'll recommend:

1. Please create a taxonomy view to show the list of "Domaine" taxonomy terms, ordered by the "Post count".
( screenshot: hidden link )

2. In this view's "Query Filter" section, include taxonomy term filter, which is linked to the shortcode attribute "terms".
( screenshot: hidden link )

3. In the "Loop Editor" section, select the "List with separators" format for the loop and include the title and post count fields so that the complete code looks like this:


[wpv-layout-start]
    [wpv-items-found]
    <!-- wpv-loop-start -->
        <wpv-loop>
            [wpv-item index=other]
                [wpv-taxonomy-title] ( [wpv-taxonomy-post-count] formations ),
            [wpv-item index=last]
                [wpv-taxonomy-title] ( [wpv-taxonomy-post-count] formations )
        </wpv-loop>
    <!-- wpv-loop-end -->
    [/wpv-items-found]
    [wpv-no-items-found]
        <strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
    [/wpv-no-items-found]
[wpv-layout-end]

4. Next, you'll need a custom shortcode, that can return the Term IDs attached to a post through its ID:


add_shortcode('get_attached_term_ids', 'get_attached_term_ids_func');
function get_attached_term_ids_func($atts) {

	$a = shortcode_atts( array(
		'id' => '',
		'taxonomy' => '',
		'seperator' => ', ',
	), $atts );

	$terms = get_the_terms( $a['id'], $a['taxonomy'] );

	if ( $terms && ! is_wp_error( $terms ) ) {

		$tax_ids = array();

		foreach ( $terms as $term ) {
			$tax_ids[] = $term->term_id;
		}

		$tax_ids_output = implode($a['seperator'], $tax_ids);

	}
	return $tax_ids_output;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.

5. After this, you'll create a new post view to show the "Formations" posts related to the "School" post where this view is shown.
( screenshot: hidden link )

7. In the "Loop Editor" section of this view too, select the "List with separators" format for the loop and include the custom shortcode registered in step 4, like this:


[wpv-layout-start]
    [wpv-items-found]
    <!-- wpv-loop-start -->
        <wpv-loop>
            [wpv-item index=other]
                [get_attached_term_ids id="[wpv-post-id]" taxonomy="domaine"],
            [wpv-item index=last]
                [get_attached_term_ids id="[wpv-post-id]" taxonomy="domaine"]
        </wpv-loop>
    <!-- wpv-loop-end -->
    [/wpv-items-found]
    [wpv-no-items-found]
        <strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
    [/wpv-no-items-found]
[wpv-layout-end]

As a result, this view will cycle through all the related formation posts, and will only return the comma-separated list of the attached "Domaine" term IDs.

7. The last step would be placing the shortcode of the taxonomy view created in step 1, in the single School custom template and passing on the attached term IDs in a shortcode attribute "terms", using the view from step 5:


[wpv-view name="view-to-show-ordered-domaines" terms="[wpv-view name='view-to-get-related-formations-term-ids']"]

Note: You'll replace "view-to-show-ordered-domaines" with the slug of the view from step 1, and "view-to-get-related-formations-term-ids" with the slug of the view from step 5.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1680931

Thank you very much for this detailed explanation.
I tried the solution but am not sure if I missed something or not.

[wpv-taxonomy-post-count]

returns total post count (whatever etablissement we're looping), where I'd need only the count for children posts of current etablissement.
Thank you.

#1681635

Hi,

Thank you for writing back and sorry about the confusion.

From your first message, I thought you'd need to show only the "Domaine" terms attached to the current school's child formations, but the sort order and term count would be global that is with respect to all formation posts.

If the sort order and the count should also be limited to child formations, then this complicates the requirement a bit and most likely will require some custom script too.

I'm going to perform some more tests and will get back to you with my findings as soon as it completes.

Thank you for your patience.

regards,
Waqar

#1688845

Hi,

Sorry about the delay in getting back on this, as we had an unusually busy queue, last week.

I just wanted to let you know that I'm still testing for some workarounds and will be able to share an update, by the end of the day.

regards,
Waqar

#1690215

Hi,

Thank you for waiting and here is a workaround that doesn't involve the usage of complex multiple views:

1. First, you'll need a custom shortcode that can get the comma-separated list of Domaines terms attached to only the related Formations from a post view and then returns the count in the descending order.

For example:


add_shortcode('show_related_domaines', 'show_related_domaines_func');
function show_related_domaines_func($atts) {

	$a = shortcode_atts( array(
		'view' => '',
	), $atts );

	$view_output = do_shortcode('[wpv-view name="'.$a['view'].'"]');

	if(!empty($view_output)) {
		
		$view_output_arr = explode(',',$view_output);
		$view_output_unique = array_count_values($view_output_arr);
		arsort($view_output_unique);

		$output = '';

		foreach ($view_output_unique as $key => $val) {
			$output .= $key.' ('.$val.' formations), ';
		}

		return rtrim($output, ', ');

	}
}

2. As explained in step 5 of the previous reply, you'll create a new post view to show the "Formations" posts related to the "School" post where this view is shown.
( screenshot: hidden link )

3. In the "Loop Editor" section of this view, select the "List with separators" format for the loop and only include the taxonomy shortcode to get the domaine terms attached to the related "Formations" in comma-separated format.


[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
			[wpv-item index=other]
				[wpv-post-taxonomy type="domaine" format="name" separator=","],
			[wpv-item index=last]
				[wpv-post-taxonomy type="domaine" format="name" separator=","]
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
	[/wpv-no-items-found]
[wpv-layout-end]

4. The last step would be to include the custom shortcode in the single School custom template and passing on the new view's name or slug in a shortcode attribute "view":


[show_related_domaines view="view-to-show-related-formations"]

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1690467

Thank you Waqar; would never have found this solution by myself.
This shortcode is beautiful.
Thank you.

#1690469

Sorry for reopening this ticket I thought solved, but I noticed a small and very strange bug on this page : hidden link
It says there are 32 formations in marketing, then another 12 formations in marketing.
I double checked and there is only one Marketing term. Also you can see the filters display 44 formations in the list for "Marketing" Domaine term...
Thank you.

#1690997

Hello,

Waqar is on vacation, I will take care this thread.

Since it is a custom codes problem, please provide your website credentials in below private message box, also point out:
Where I can edit your custom PHP codes.

I need a live website to test and debug the problem, thanks

#1691647

Hi,
Another small bug I suspect to be similar to last one is when there is no result, it keeps displaying "(1)".
Example here : hidden link
Thank you.

#1692435

Thanks for the details, I can login your website.

We can handle the questions one by one:
It says there are 32 formations in marketing, then another 12 formations in marketing.
in the problem URL you mentioned above:
hidden link

Which one is the "32 formations in marketing"? which one is the "12 formations in marketing"?

Where and how do you display the shortcode [show_related_domaines]?

#1692633
toolset-domaines-formation.JPG

Please first login in order to see the content.
Then on the page scroll down to "Domaines de formation" section.
There are 3 issues here :
A- The list is split in two parts, one before the title, another after the title. All results must display after the title.
B- On the top part, you can see "Marketing" is listed 2 times, with 44 formations splited in 32 + 12 (yellow on the screenshot).
C- Finally, if there is no formation, it keeps displaying a "(1)" instead of nothing.
Thank you.

#1692645

Where do you put the custom shortcode [show_related_domaines]? in page content or view content? please point out the URLs , thanks

#1692817

Hi,
Here it is : hidden link, line 34
Note this content template is called on several views and content templates, but mainly on this one: hidden link (which is the default content template for single etablissement)