Skip Navigation

[Resolved] Can’t query for children and taxonomy

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

Problem: Post Type A and Post Type B are related in a many-to-many relationship by Post Type C (intermediary post type). On the Post Type A single post page, I would like to display a View of all related Post Type Bs, filtered by a taxonomy term.

Solution:
- Create a View of Post Type C, filtered by post relationship, where C is a child of the current post (Post A).
- It is not possible to filter Post Type C based on Post Type B taxonomy terms, so instead you can use conditional logic to show or hide each result based on Post Type B's taxonomy term (in this example, the term is "photographers", the taxonomy is "listing_cat", and Post Type B is "listing").

[wpv-layout-start]
    [wpv-items-found]
   <h4>Category Header</h4>
   <ul class="wpv-hide-empty-header">
    <!-- wpv-loop-start -->
    <wpv-loop>
        [wpv-conditional if="( has_term('photographers', 'listing_cat', [wpv-post-id id='$listing']) eq true )"]<li>[wpv-post-link id="$listing"]</li>[/wpv-conditional]
    </wpv-loop>
    <!-- wpv-loop-end -->
    </ul>
    [/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]

- Go to Toolset > Settings > Front end Content and find the "Functions inside conditional evaluations" section. Add has_term here.
- If empty category headers are appearing, you can apply the following JS:

jQuery(document).ready(function(){
  jQuery('.wpv-hide-empty-header').each(function(index, item) {
    if(jQuery(item).find('li').length < 1) {
      jQuery(item).prev().hide();  
    }
  });
});

Relevant Documentation:
https://toolset.com/documentation/toolset-training-course/part-9-many-to-many-relationships-in-toolset/
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

This support ticket is created 6 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 24 replies, has 2 voices.

Last updated by Karla Jacobsen 6 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#550134

Okay I see the empty category issue you're talking about now. There's really not a good way to accomplish that with Views filtering and conditional logic. I could provide a JavaScript solution that will hide all the empty section headers if that's something you might be interested in.

#550160

Yes, that would be great!

#550357

Okay I have added this JavaScript in Layouts CSS and JS:

jQuery(document).ready(function(){
  jQuery('.wpv-hide-empty-header').each(function(index, item) {
    if(jQuery(item).find('li').length < 1) {
      jQuery(item).prev().hide();  
    }
  });
});

I have made one modification in the Category-ApparelAndAccessories View. I added the class "wpv-hide-empty-header" to the ul just after the Apparel and Accessories h4 tag. Now this h4 element will be hidden if both these conditions are met:
1. The ul contains no li items, i.e. no results are found in the View.
2. The h4 tag is the immediately previous sibling of the ul tag.

To apply this fix to all the other Views used in the Shows template, you must make sure the h4 tag is just before the ul tag, and you must add the class "wpv-hide-empty-header" to the ul tag in that View's Loop Output editor. This fix will apply to any page of your site that uses a Layout.

Let me know if you have any questions.

#550474

Almost, but not quite...the new function seems to be causing a side effect of hiding all my listings (hidden link). Not sure why that might be happening.

Also, it leaves a newline for each empty category. Can that be fixed?

#550479
Screen Shot 2017-07-19 at 1.33.59 PM.png

Almost, but not quite...the new function seems to be causing a side effect of hiding all my listings (hidden link;
I'm pretty sure you have some other issue here, it's unrelated to the code I put in place. As a test, I have commented out the code in Layouts CSS and JS and there were still no results displayed here. Screenshot attached. You can delete all the code there if you'd like to test this out. I've reverted the code for now so you can work on the other issue, empty spaces.

Also, it leaves a newline for each empty category. Can that be fixed?
I can help with this if you modify your Layout and place all the Views in a single container. You can do this by replacing all the existing View cells from multiple rows with a single Visual Editor cell, then placing all the individual Views inside that editor cell using the Fields and Views button to generate shortcodes. Otherwise you have a lot of HTML markup surrounding each section that is going to cause empty space.

#550496

Newlines - Yes, please help. Thank you!

Any ideas about why the listings stopped loading? It was working last night, and the only thing that I've changed is the show layout and adding all the new views.

Note:
I say that everything was working fine last night, but there is one caveat to that statement. The links below all loaded correctly, however, there was a weird problem with the first link. It would take forever to load and sometimes hang. If I refreshed, it would generally reload fine. Unlike today's problem though, the whole page would be blank, rather than just failing to lead the listings like it is today.
hidden link
hidden link
hidden link

#550506

I figured out the problem with the missing listings, although it revealed what I believe is a bug. It's a problem that I've hit several times now. I think it's caused when duplicating views. Should I describe the bug here, or is there a better place to report it?

#550513

Please open a separate ticket for the duplicated Views issue you mentioned - that will need to be investigated and tracked on its own. Before you submit that ticket, I recommend enabling server logs to see if there are any server-side errors causing problems. Go in your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:

define('WP_DEBUG', true);

Then add these lines, just before it says 'stop editing here':

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

Reload the vendors page a few times. If you get a white screen because of a server error, this will create an error_log.txt file in your site's root directory. Attach its contents to your new ticket. Once that is done, you can revert the updates you made to wp-config.php.

I'm taking a look at the newlines now, standby.

#550516
Screen Shot 2017-07-19 at 2.57.59 PM.png

Sorry I must not have been very clear about what I need from you to resolve the empty space / new line issue. Take a look at the screenshot posted here. Each of these View cells is contained in a separate Layout row. This is part of the problem, because each row is surrounded in HTML markup, contributing to the spacing issue you have noticed.

I need you to remove all these rows (from Row 9 down to the very last row). Then create a new row with a Visual Editor cell in it, and place all your Views inside that Visual Editor cell. You can place your cursor in the Visual Editor cell, then click the "Fields and Views" button to insert each View.

This will prevent extra markup from showing around each "row", and help resolve the spacing issue. Let me know if you have questions or concerns about this.

#551064

Got it! I still have to go through and finish adding them all again, but that looks like it worked.

And I will check out the error log that you suggested as well.

Thank you so much for all your help.

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