Skip Navigation

[Resolved] Looking for ideas on showing categorising child posts for parent posts

This support ticket is created 6 years, 12 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)

Tagged: 

This topic contains 16 replies, has 2 voices.

Last updated by StanleyT8485 6 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#601949

Tell us what you are trying to do?

I am building a directory of mining companies.
Companies = Parent
Mines = Child

I want to categorize child posts for parent posts in this structure (hidden link). This table categorizes the mines by commodities (Gold, Silver, Copper, etc.)

For example,
GOLD
- Mine A
- Mine B

SILVER
- Mine B
- Mine C

Copper
- Mine C

TRICKY PART - The tricky part here is different companies have different commodities. For example, some only have Gold and Copper while some may have Lithium, Copper, and Coal.

What I'm doing now is adding all the commodities for the company as custom fields and using shortcode for each custom field. Like this - '[wpv-view name="display-mines-for-company" mineral="[types field='co-mineral'][/types]"]'

I'm sure there is a more efficient way to do this.

How would you categorize child posts in this manner?

#601971

Nigel
Supporter

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

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

Hi Stan

From your initial description of the data, Company CPT > Mine CPT, where each mine can produce different commodities, then that looks to me like you should have a commodity taxonomy assigned to mines.

Then you can automatically have taxonomy archives that show all mines which produce commodity gold, for example, and if you want to summarise this info you can create a taxonomy View which displays commodity terms (so it will list Gold, Silver, Copper etc.) and then you can have a nested mines post View with a taxonomy Query Filter where the term is from the parent View.

That would give you the results you describe and it then just becomes a question of formatting etc.

Is that what you need?

#602066

Hey Nigel,
Your solution with taxonomy works but only for the first part.

TRICKY PART is categorizing the mines in the parent posts because different companies have different commodities. For example, some only have Gold and Copper while some may have Lithium, Copper, and Coal.

And formatting the company page in this way - hidden link. This table categorizes the mines by commodities (Gold, Silver, Copper, etc.)

Solution #1 is to manually check what commodities does the company produces and use a View for each commodity.
For example,
Company A
[wpv-view name="view-name" mineral="Gold"]
[wpv-view name="view-name" mineral="Silver"]

Company B
[wpv-view name="view-name" mineral="Lithium"]
[wpv-view name="view-name" mineral="Copper"]

The problem with this solution is you have to manually go through all the companies (3,000 in my case) and attach the View.

The ideal solution for me here is:
1. Check commodity of all child posts
2. Use Views to output mines categorized by commodities in this format (hidden link)

How would you do it, Nigel?

#602581

Nigel
Supporter

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

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

Screen Shot 2018-01-02 at 12.10.29.png

Hi Stan

Sorry for the delay getting back to you after the holidays.

I set up a test site to double-check what will work.

Here's what I created.

A Content Template for companies with a View "commodities" which lists all of the commodity terms, no filters. In the Loop Output section I insert a heading with the taxonomy title, and a second View "mines-of-current-taxonomy-and-company".

This View shows mine posts, and has two filters, a taxonomy filter to "Select posts with taxonomy: Commodities set by the parent Taxonomy View", and a post relationship filter to "Select posts that are children of the Post where this View is shown".

That works inasmuch as the results are displayed correctly (see screenshot).

The inner View can display nothing if no results are found rather than the text, but that still leaves you with the commodity title when there are no mines for that commodity for the company in question.

To not display that you could either come up with a custom PHP function that you use in a conditional shortcode which essentially reproduces the inner View to determine whether there are any results to display or not, or you could not show the headings at all, so the outer taxonomy View displays nothing itself and is simply a container for the inner View, which displays the commodity in the first column so that they are clearly all grouped together.

#602798

Hey Nigel,
Thanks for this.

I had that same idea too and test that out. It worked but do you think there's another more efficient way of doing this?

The reasons are because we have over 3,000 companies and 33 different commodities and there will be a lot of "No results found". With this, our content template has 33 Views, each querying the different commodities in our database.

Would Views be able to view all minerals of child posts and output that into the results? Instead of manually entering each commodity in the content template. The benefit, if we can achieve this, is the parent posts will automatically add new commodities to the parent page if we add any new child posts with new commodities that didn't exist before.

Looking forward to your ideas, Nigel.

#602913

Nigel
Supporter

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

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

Hi Stanley

I don't understand why your template has 33 Views, unless you mean you are doing it differently than I suggested.

The solution I described requires 2 Views. One View which retrieves the commodities (including any new ones you subsequently add), and a nested View which displays the corresponding mines for that commodity and company.

Regarding the No Items Found text, you can simply output nothing at all for no items found, which just leaves the problem of the heading for the commodity when there are no mines for that commodity.

The simplest solution would probably to add a classname to that heading and then use jQuery to check the length of the subsequent div to see if it is zero (assuming you output nothing for no results, as described above), in which case you hide the heading.

Does that sound like a viable solution?

If you try that and need help, let me know.

#603283
sample.png

Hi Nigel,

Can you show me a screenshot of the test site or give me access to this test site to check out your solution?

I have attached a screenshot of how I want it structured. And so far the only way I can think of is using separate Views for each commodity.

Stan

#603346

Nigel
Supporter

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

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

mining.gif
mines-filters.png
commodities-content.png

My Content Template for a single company inserts a View called "commodities".

This View displays commodity taxonomy terms (see commodities-content.png).

The Loop Output section looks like this:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
          <h2>[wpv-taxonomy-title]</h2>
          [wpv-view name="mines-of-current-taxonomy-and-company"]
		</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]

I'm outputting the title of the commodity, then inserting a second View called "mines of current taxonomy and company".

This second View displays the mines post type, and includes two Query Filters, as shown in mines-filters.png, one for the current commodity (the outer View loops through the commodities) and another a post relationship filter to show mines that are children of the company being shown.

In my crude example the Loop Output section simply displays the mine post title.

You can see the results in the gif.

It has the problem of the "no results found" when there is no mine for a particular commodity, and I described some possible solutions for that above.

In terms of your solution, where you say you are using 33 Views, you could at least reduce that to a single View and pass the commodity as a shortcode attribute (https://toolset.com/documentation/user-guides/passing-arguments-to-views/).

You would then insert the same View as many times as needed, changing only the attribute for the commodity, which should make your site maintenance a little easier.

So for this solution, edit your company template and insert the single View as many times as you have commodities, like this:

[wpv-view name="mines-filtered-by-commodity-attribute-and-parent-company" wpvcommodity="gold"]
[wpv-view name="mines-filtered-by-commodity-attribute-and-parent-company" wpvcommodity="silver"]
[wpv-view name="mines-filtered-by-commodity-attribute-and-parent-company" wpvcommodity="copper"]
[wpv-view name="mines-filtered-by-commodity-attribute-and-parent-company" wpvcommodity="lithium"]
[wpv-view name="mines-filtered-by-commodity-attribute-and-parent-company" wpvcommodity="coal"]

You can move displaying the name of the commodity into the View itself, and output nothing if no results are found. So the Loop Output section of the View would look a little like this:

[wpv-layout-start]
	[wpv-items-found]
	<h2>[wpv-attribute name="wpvcommodity"]</h2>
	<!-- wpv-loop-start -->
		<wpv-loop>
          <h3>[wpv-post-link]</h3>
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
	[/wpv-no-items-found]
[wpv-layout-end]

Note how I output the title of the commodity using the wpv-attribute shortcode, but it will only display if there are any mines to show for this commodity. The only drawback is that will output the slug rather than the name, but you could add a simple CSS rule for the titles which use text-transform: capitalize; to turn gold into Gold etc.

#603703

Sorry Nigel,

Can you show me some screenshot of your second View?
And did you use any filters in your main View?

This is my first time creating a View inside another View.

#603712

Nigel
Supporter

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

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

Hi Stanley

There's not really anything to add about the second View, here is what I wrote before:

This second View displays the mines post type, and includes two Query Filters, as shown in mines-filters.png, one for the current commodity (the outer View loops through the commodities) and another a post relationship filter to show mines that are children of the company being shown.

In my crude example the Loop Output section simply displays the mine post title.

The post type is mines, and there are two query filters, as shown in the screenshot.

There is no magic when it comes to creating nested Views.

The Views are separate and can be edited separately.

The nesting happens simply when you insert one View (using the Fields and Views button to insert the wpv-view shortcode) inside the Loop Output section of another View, as I showed in the previous reply.

I actually think that, because of the problem of displaying commodity titles even when there are no mines for that commodity, you might be better served by the alternative solution I described towards the end.

#603721

How about the first View? Did you use any filters in that? And what did you put in the loop output?

#603731

Nigel
Supporter

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

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

Hi Stanley

The outer View loops over all commodity taxonomy terms, there are no filters.

I already pasted the whole Loop Output section of that View above, but let me copy it again here:

[wpv-layout-start]
    [wpv-items-found]
    <!-- wpv-loop-start -->
        <wpv-loop>
          <h2>[wpv-taxonomy-title]</h2>
          [wpv-view name="mines-of-current-taxonomy-and-company"]
        </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]

It contains just the commodity title and the inserted second View (the name of your View will likely be different).

#603736
first view.png
second view.png

Sorry Nigel,

I'm very lost with this.

I have attached a screenshot of two of my views.
First view with query filters and loop output.
Second view with the loop output only.

Note #1: I use "Commodity" as my Tags.

Note #2: In this example, parent is Mine and child is the commodities of the Mine.

And here is a result I'm getting: hidden link

Morenci mine only has Copper and Molybdenum but it is showing Bauxite, Gold, Zinc, Lead, etc.

#603751

Nigel
Supporter

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

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

Hi Stanley

We seem to be talking at cross purposes here.

I understood that you were asking to show a Company, and for that Company list the mines belonging to that Company organised by commodity (e.g. list the silver mines, then the gold mines etc.).

The page you have linked to is a Mine, and I'm not sure what you intend to display on that page.

Can you clarify?

#603914

Hey Nigel,

Yes, the example I gave was for a Company (Parent) and Mine (Child).

What I'm working on is also the same where Mine is now the parent and Mine's Commodity is the child.
For example,
Morenci (Parent)
- Morenci Copper (Child)
- Morenci Gold (Child)

What I intend to display on the page is what you just did, a breakdown by resources.

Gold
[insert Gold table for mine here]

Copper
[insert Copper table for mine here]