Skip Navigation

[Resolved] only display parent taxonomies when child item has certain text in string

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

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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+01:00)

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 7 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#404685

For example:

I have multiple houses from a certain type of building. So, each type of building has several houses. I added a variable field to houses to set the status of it. For example: 'sold. or 'for sale'.

At this page:
hidden link

I only want to show types of buildings, from which there is any house for sale. So if there is type of building where there's no house left, just hide the whole building type.

Can you help me arrange that?

#404782

Nigel
Supporter

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

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

Screen Shot 2016-06-06 at 15.56.23.png

If I have understood you correctly,

- you have a custom post type Buildings which is parent to another post type Houses
- houses have a status custom field which can be 'sold' or 'for sale'
- you have a View to display buildings which should only include buildings that have child houses for sale

You are effectively reproducing the option from taxonomies to not display a taxonomy term if there are no posts attached, but using custom post types.

If you are comfortable creating custom code, such as writing a custom shortcode, then I would suggest that the optimal solution is to create the View which shows all buildings, write a custom shortcode that returns the number of child house posts with status of for sale, and use that to conditionally display the required content for your building. (For more on the views conditional shortcode see: https://toolset.com/documentation/views-shortcodes/#wpv-conditional)

But from your question I'm guessing you are looking for a solution which doesn't require custom coding. It's less intuitive, but here's how it works:

- Make a View for buildings that you will add to the page where you want it displayed. The View will display all buildings, not restricted by any filters. Include your ordering preferences.
- We are going to nest a View of Houses in the Loop output, but we need to create it first.
- Create the View that will show Houses for sale. We need to add 2 query filters so that we only return houses for the current Building (from our outer View) and which are for sale. See the attached screenshot.
- So, add a post relationship filter to select posts which are children of the current post in the loop, and
- Add a custom field filter to select posts where the status field is set to for-sale (your naming conventions may differ)

Now for what is output. Although we want to output buildings, our buildings view won't directly output anything. In the Loop output for the buildings View, just add a shortcode for the houses-for-sale view.

Now, in the Loop output for houses-for-sale we can add id="$building" to our shortcodes to say that we don't want to output content from the current post, which in our nested loop is a house, but rather from the parent building post. Here, in my houses for sale view output I am just displaying the building title, but you can design your output as you please:

	<!-- wpv-loop-start -->
		<wpv-loop>
          <p>[wpv-post-title id="$building"]</p>
		</wpv-loop>
	<!-- wpv-loop-end -->

So, we loop through the buildings, and for each one we loop through its child houses that are marked for sale and for those print, not their own content, but the content of their parent building.

Now, if you haven't picked up on the likely problem already you will in testing. If a building has multiple child houses that are for sale then the building title we be printed for each of them, i.e. will be repeated.

So the final part of the solution is that in the Limit and Offset section of our houses for sale view you specify that we only want to display 1 item, i.e. we are not interested if there are multiple houses for sale for that business, just whether there are 0 or 1+.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.