[Resolved] hiding parent posts when there are no child posts attached
This thread is resolved. Here is a description of the problem and solution.
Problem:
hiding parent posts when there are no child posts in view result
Solution:
To hide the parent posts if there is no child while using the nested view, you will have to use the view's filter hook "wpv_filter_query_post_process" where you can remove the parents based on child is available or not.
To find the related child posts, we can use the post relationship API function toolset_get_related_posts() or WP_Query.
Minesh had helped me previously with a piece of code that filters out all parent posts in a view, if there are no child posts inside the parent post. Thread: https://toolset.com/forums/topic/exporting-posts/
The code works nicely, but I just realized that when I apply a filter and hit the "search" button, it seems to show any parent post result even if there is no child post nested inside that result. Is it possible to make this snippet work even for when someone uses the filters?
Hello. Thank you for contacting the Toolset support.
Can you please share problem URL and admin access details. Basically, I see the previous ticket there was no custom search filter involved at that time.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Can you please share few instance with what field what should I try to search using which I can see the search result where it includes posts without child.
Type in the word "bais" into the first filter named "Enter Name of Shul" - and hit enter. Once you see the results, you'll see the 3rd result on the first row has no Shiur/Child Post inside, but still shows up.
I searched using the keyword: "bais"
=> hidden link
I can see the third box do not display the child post, but I found that you have wrapped the whole loop output for the nested view with the [wpv-conditional] statement:
=> hidden link
I see the third box "Bais Hamedrash Chaim-Reuvan-Nitra" does not display the child post but If I try to add the [wpv-post-link] shortcode outside the [wpv-conditoinal] shortcode, it does display the related child post "Feish Herzog’s DAF YOMI".
Actually, its your conditional statement that hides those results.
I see. The thing is that I need my conditional statement to be there, I will explain to you and maybe we can figure out a workaround?
Basically, the conditional statement hides all related child posts that have a specific taxonomy. If I don't hide all related post with this taxonomy, then I will have many duplicates. Because child posts have a many-to-many relationship, and the same post can be displayed twice. The best way that I could think of to filter this issue out was to use the taxonomy, because if a post is related, it also gets checked as either taxonomy A or taxonomy B. So I hid taxonomy B, and now I am left with no duplicates.
Is there any way you can think of this implementation working together with the snippet of code?
so, I searched using the keyword: "bais"
=> hidden link
I see the third box "Bais Hamedrash Chaim-Reuvan-Nitra" does not display the child post, so do you mean that I should hide this post as well "Bais Hamedrash Chaim-Reuvan-Nitra" (this post should not be displayed) if the taxonomy term is assigned "daf-yomi" from topic taxonomy?
I mean that that parent post should be hidden because the child post that is currently not being displayed does not have the taxonomy term "daf-yomi" assigned. I want to hide all parent posts (locations) where the child post is not daf-yomi, since that is anyways what I am hiding with the conditional statement.
so, I searched using the keyword: "bais"
=> hidden link
I see the third box "Bais Hamedrash Chaim-Reuvan-Nitra" does not display the child post,
I checked the child post "Feish Herzog’s DAF YOMI": hidden link
-- I can see for above post the term "daf-yomi" is assigned.
But it seems its not displaying because, you have two conditional statements added to the following view that displays the child posts:
=> hidden link
[wpv-conditional if="( '[wpv-post-taxonomy type='topic' format='slug']' eq 'daf-yomi' )"] [wpv-conditional if="( $(wpcf-contact-role).item(@person-to-shiur.parent) eq 'Maggid Shiur' )"]
[php]
so, the first condition for 'daf-yomi' is working. but it looks like it fails for second condition:
[php]
[wpv-conditional if="( $(wpcf-contact-role).item(@person-to-shiur.parent) eq 'Maggid Shiur' )"]
I can see the contact role for the post ""Feish Herzog’s DAF YOMI"" is set as Contact:
- hidden link
As I understand, you only want to display the parent post if both conditions are satisfied - is that correct?