Problem:
Tell us what you are trying to do?
1. I have a custom post type of "podcast"
2. I have a custom post type of "book"
3. I have a relationship established between the above two custom types
4. In some podcast posts, I indicate one or more related books, so that I can display books that I mention in each podcast.
5. For podcasts that have related books, I show them on the podcast post page. This is all working fine using the amazing relationship tools built into Toolset.
6. Now, for podcasts that have related books, I want to add a heading above the related books that I display called "Books mentioned"...but
7. I don't want that heading to display IF the podcast post doesn't have any related books.
I already have done steps 1-5 and need info on how to accomplish 6-7 using existing Toolset blocks, functions, shortcodes, etc., whatever is easy and most efficient.
Is there any documentation that you are following?
I've been digging into some queries and shortcodes like wpv-control-post-relationship, but I'm not quite sure if that's the right direction, plus I'm not really a coder. The conditional block doesn't seem to work since there I no options I see to check for the data count of related books.
Is there a similar example that we can see?
What is the link to your site?
Attached is the tree structure of what I have in place. The "Fields and Text" block is the one that contains the heading. However it appears on all podcast post pages, regardless if there are related books or not. I need it to not appear on these posts when there are no related books to the podcast.
Solution:
There's not really a simple way to test whether or not the current post has related posts, using a simple conditional. In general, the most straightforward way to determine whether or not related posts exists is to use a View. Assuming you have already implemented a View of related posts, the View will display the related posts if any exist.
Having this is mind, could you please try the following steps?
- Visit the page/template you want to set this condition
- Move the Headline (or the 'Fields and Text' block) into the View, but not in the Loop area. Move it into the Search filters area instead. For this, please turn on the "Search" option in the top-level View block to expose the View Search block, where you would normally display search filters: https://snipboard.io/pP0LQ3.jpg
- Drag your conditional block into that search area
- Then you need to set up the conditions to test whether the number of posts found (Views shortcode) is greater than a static value of 0 (the number zero). Like this: https://snipboard.io/bPnD0q.jpg
- Now move the heading into this condition block and save the post
This should be enough to hide the heading if the post does not have a relationship. If you also want to hide the 'No items found' text:
- When you have the View Loop block highlighted, you can delete the text in the Loop Template settings for the 'No items found' text. Screenshot showing these steps: https://snipboard.io/12Vutc.jpg
Then just save and you should have the desired results.
Relevant Documentation:
https://toolset.com/forums/topic/display-related-posts-only-if-there-are-any/
https://toolset.com/course-lesson/using-toolset-conditional-block/