Skip Navigation

[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.

You can find proposed solution in this case with the following reply:
https://toolset.com/forums/topic/hiding-parent-posts-when-there-are-no-child-posts-attached/#post-2325785

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#new-approach

This support ticket is created 2 years, 1 month 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 11 replies, has 2 voices.

Last updated by simchaH 2 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#2318897

Hello,

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?

Thanks!

#2319051

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#2322643

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#2323175

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.

Thanks!

#2323993

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#2324057

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?

#2324697

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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?

#2325087

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.

#2325611

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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?

#2325665

Yes, that is correct.

Am I able to place that second conditional elswhere, perhaps in the parent view and it will work to hide the whole parent post?

Thanks!

#2325785

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You are using really complex post relationship and this was a challenging task.

I've adjusted the code as given under to "Custom code" section offered by Toolset:

add_filter( 'wpv_filter_query_post_process', 'func_filter_child_none_pagination_post', 10, 3 );
function func_filter_child_none_pagination_post( $query, $view_settings, $view_id ) {
  
    if ($view_id==1152) { 
          
      $all_posts = $query->posts;
   
      foreach($query->posts  as $k=>$v):
        
      // getting all related posts 
        $parent_ids = toolset_get_related_posts(
                         $v->ID,
                        'shiur-location-to-shiur',
                        'parent',
                        1000000,0,
                        array(),
                        'post_id',
                        'child'
                      );     
  
	// getting all posts where tax term == daf-yomi
      $loc_shiur = new WP_Query(array(
            'post_type' => 'shiur',
             'fields' => "ids",
            'posts_per_page' => -1,
            'toolset_relationships' => array(
                       'role' => 'child',
                       'related_to' => $v->ID,
                       'relationship' => 'shiur-location-to-shiur'
            ),
             'tax_query' => array(
                      array(
                          'taxonomy' => 'topic',
                          'field'    => 'slug',
                          'terms'    => array('daf-yomi'),
                          'operator' => 'IN')),

            )
);
$found_loc_shiur =  $loc_shiur->posts;
  
foreach($found_loc_shiur as $x=>$y):
        
        $found_people_shiur = toolset_get_related_posts(
                         $y,
                        'person-to-shiur',
                        'child',
                        1000000,0,
                        array(),
                        'post_id',
                        'parent'
                      );     
      
        foreach($found_people_shiur as $p=>$q):
      
        $people_shiur = new WP_Query(array(
                'post_type' => 'person',
                'fields' => "ids",
                'posts_per_page' => -1,
          		'post__in' => array($q),
                'meta_query' => array(
                          array(
                              'key' => 'wpcf-contact-role',
                              'value'    => 'Maggid Shiur',
                              'compare' => 'LIKE')), 

                ));
       		 $has_role =  $people_shiur->posts;
            
        endforeach;
      
endforeach; 
             if(empty($parent_ids) or empty($has_role)){
              	unset($all_posts[$k]);
            }
 	
 
      endforeach;
     
     
 		$all_posts = array_values($all_posts);
      
       $query->posts = $all_posts; 
       $query->found_posts = count($all_posts); // modify the count of found posts
       $query->post_count = count($all_posts); // modify the count of displayed posts
    }
    return $query;
}

If I search using the the keyword: "bais"
==> hidden link

I can see the box that used to display i.e. "Bais Hamedrash Chaim-Reuvan-Nitra" without child post is now not get displayed.

#2326121

Minesh you are the best. My issue is resolved now. Thank you!

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