Skip Navigation

[Resolved] Split: Hiding parents with no children – show pagination or adjust pagination when hiding parent post when no related child available

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 4 replies, has 2 voices.

Last updated by Minesh 10 months, 3 weeks ago.

Assisted by: Minesh.

Author
Posts
#2688114

Hello Minesh,
Your solution works, but there is a strange behaviour of the pagination. I set the pagination to max 5 parents per page in the parents view and in output the first page is empty while other pages show one parent only (with correct children) and many other empty pages afterwards. Removing the pagination the output is correct.
Please let me know.
thanks

#2688117

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 where you are displaying the view as well as admin access details.

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

#2688122

Minesh
Supporter

Languages: English (English )

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

Thank you for sharing admin access details. The admin access details works but:

When I try to login using the frontend user: Hamburger - it says ""Password is incorrect!".

Can you please send me working frontend user access details.

I have set the next reply to private which means only you and I have access to it.

#2688125

Minesh
Supporter

Languages: English (English )

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

Still the password you shared does not work. Can you please send me working frontend user access details.

And I get the following message with white page;

Your access to this site has been temporarily limited by the site owner

Your access to this service has been temporarily limited. Please try again in a few minutes. (HTTP response code 503)

If you think you have been blocked in error, contact the owner of this site for assistance.

I have set the next reply to private which means only you and I have access to it.

#2688384

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've added the code snippet "remove-parent-without-child" to "Custom Code" section offered by Toolset:
- hidden link

add_filter('wpv_filter_query', 'func_filter_parent_having_no_child_post', 101, 3);
function func_filter_parent_having_no_child_post($query, $view_settings, $view_id) {
  $views = array( 20559 );
  $relationship_slug = 'pub-event';
  $parent_type_slug = 'pub';
  // you should not edit anything below this line
  if ( in_array( $view_id, $views ) ) {
    
    $exclude_ids = array();
    
    /// get all parents
    $parents_args = array(
      'post_type' => $parent_type_slug,
      'numberposts' => -1,
    );
    $parents = get_posts( $parents_args );
    
    foreach($parents as $parent) {
      $children = toolset_get_related_posts(
        $parent->ID,
        $relationship_slug,
        'parent',
        99999,
        0,
        array(),
        'post_id',
        'child'
      );
      
      if( !is_array($children) || count($children) < 1 ) {
        array_push( $exclude_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'], $exclude_ids );
  }
  return $query;
}

For parent view - I've adjust the view's "Loop Editor" section as given under:
- hidden link

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop>
		[wpv-post-body view_template="loop-item-in-planned-events-pubs-parent"]
	</wpv-loop>
<!-- wpv-loop-end -->
[wpv-pager-nav-links output="bootstrap" first_last_links="true" previous_next_links="true" text_for_previous_link="Precedente" text_for_next_link="Successivo" text_for_first_link="Primo" text_for_last_link="Ultimo"]
	[/wpv-items-found]

	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]Nessun locale che sta pianificando eventi trovato.[/wpml-string]</strong>
	[/wpv-no-items-found]

[wpv-layout-end]

For child view - I've adjust the view's "Loop Editor" section as given under:
-hidden link

[wpv-layout-start]
	[wpv-items-found]
		
	<!-- wpv-loop-start -->
<table>
	<wpv-loop>
      [wpv-post-body view_template="loop-item-in-planned-events-events-children"]
	</wpv-loop>
</table>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"][/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

Can you please confirm it works as expected now.

#2688429

Hello Minesh,
you solution is brilliant and works perfectly as far as I can see. I wouldn't ever be able to do it without your help. I understand in PHP you count the children before filtering the parents, I tried to do the same with a view but then I couldn't find any way to filter. I suppose hiding parents with no children could be common issue for other users too so please write and publish a generic documentation of your solution or - even better - ask developers to implement the feature in the view code so that it will be easy to choose the option if needed. Thank you very much.
Kind regards
Nicola