Skip Navigation

[Resolved] Exclude all Posts whit no Children in a View

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

Problem:
How to filter parent posts having no child or How to display only parent posts having no children

Solution:
To display the only parent post not having any child post, You need to use the view's query filter: wpv_filter_query in conjunction with the Toolset post-relationship API function where using post-relationship API function toolset_get_related_posts() you will be able to find the child posts based on the specific parent post.

You can find the proposed solution, in this case with the following reply:
=> https://toolset.com/forums/topic/exclude-all-posts-whit-no-children-in-a-view/#post-1625509

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

This support ticket is created 4 years, 8 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
- 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 7 replies, has 3 voices.

Last updated by Minesh 3 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#1623881

In my project I have a producer and products with a parent (producers) and child (products) relation. In the view, in which I show the producers, I want to hide all posts that have no children. Can you help me to find the right query filter?

This is the Site where the view is displayed: hidden link

Thanks

#1624229

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

There is no way to display the parent post not having the child post directly. You need to use the view's query filter: wpv_filter_query in conjunction with the Toolset post-relationship API function.

As you are using filters as well with your view so its hard to find the only posts which do not have any child as we need to consider the search input as well.

Can you please share access details so I can review your setup and check what is the best way to implement it.

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

#1625509

Minesh
Supporter

Languages: English (English )

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

I checked on your search page: hidden link

I see you reusing the text search and you can do filter the view using ajax when you add a text search, that is why I've added the search button and with full refresh when you click on the search button.

I've added the following code to the "Custom Code" section offered by Toolset to filter the posts who do not have any child.
=> hidden link

add_filter('wpv_filter_query', 'func_filter_parent_where_no_child', 101, 3);
function func_filter_parent_where_no_child($query, $view_settings, $view_id) {
  $views = array( 5464 );
  
  $relationship_slug = 'winzer-weine';
  $parent_type_slug = 'winzer';
  
  // you should not edit anything below this line
  if ( in_array( $view_id, $views ) ) {
    
     $ids = array();
    $parents_args = array(
      'post_type' => $parent_type_slug,
      'numberposts' => -1,
      'meta_query'=> $query['meta_query'],
      's'=> $query['s'],
      );
    $parents = get_posts( $parents_args );
    
    foreach($parents as $parent) {
      $children = toolset_get_related_posts(
        $parent->ID,
        $relationship_slug,
        'parent',
        1000000,
        0,
        array(),
        'post_id',
        'child'
      );
      if( !is_array($children) || count($children) < 1 ) {
        array_push( $ids, $parent->ID );
      }
    }
    
     $query['post__not_in'] = isset($query['post__not_in']) ? $query['post__not_in'] : array();
    $query['post__not_in'] = array_merge($query['post__not_in'], $ids );
  }
  return $query;
}

Can you please confirm it works as expected?

#1625869
Screenshot 2020-05-15 at 15.55.24.png

Thanks for responding so quickly.

Is there a special reason why you changed the search to full page load? The search box worked just as well with Ajax.

It doesn't seem to be working properly yet. The posts, which I marked in red in the attached picture, should disappear.

#1628459

Minesh
Supporter

Languages: English (English )

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

I checked on this page: hidden link

I do not see the posts you marked with the border. Can you please tell me on what page I can see those posts? Do I need to apply any filter?

Is there a special reason why you changed the search to full page load? The search box worked just as well with Ajax.
==>
Yes, as you are using text search, text search do not support ajax search and it needs search button that is why you need to go with search button.

#1628515

I don't see the posts anymore either, so we can conclude that there are no more posts displayed that don't have children. Thank you very much for the excellent support. Have a nice day.

#1947941

Ed

Minesh,
How can I use this code for 3 different views (States, Counties, Cities)?

I don't want to show States, Counties, or Cities, that have no Listings.

#1948507

Minesh
Supporter

Languages: English (English )

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

@Ed - Please open a new ticket.