Skip Navigation

[Resolved] Grid of parent posts, with lists child posts for each parent + hide empty

This support ticket is created 4 years 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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

Last updated by stewartE 4 years ago.

Assisted by: Minesh.

Author
Posts
#1817973
wrong.jpg
correct.jpg
1705147-grid.png

Hi support originally helped me with this ticket "Create a grid of parent posts, with lists child posts for each parent" -
https://toolset.com/forums/topic/parent-child-posts-display-all/ Basically creating a grid of parent posts with their child posts underneath (see grid.png screenshot)

which worked perfectly. I've since come up against a slight problem. I hadn't anticipated that there would be some parent posts which didn't have any child posts. In that case the grid shows the standard "no items found" text which looks a bit odd. Is it possible to hide the parent AND child if the parent has no children.

#1818717

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Yes, its possible but I would like to review how you setup your nested views.

Can you please share problem URL where you added the parent/child grid as well as 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.

#1821055

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've added the following shortcode "get-related-post-count" that will help us to find out if there is any child post available for the parent in loop to "Custom Code" section offered by Toolset:
=> hidden link

add_shortcode( 'get-related-post-count', function( $atts = [] ){
   
    // provide defaults
    $atts = shortcode_atts( 
        array(
            'relationship'      =>   '',
        ), 
        $atts
    );
   
    global $post;
    $count = 0;
   
    $relationship = toolset_get_relationship( $atts['relationship'] );
   
    if ( $relationship ) {
   
        $parent = $relationship['roles']['parent']['types'][0];
        $child = $relationship['roles']['child']['types'][0];
        $type = $post->post_type;
   
        $origin = ( $parent == $type ) ? 'parent' : 'child';
   
        // Get connected posts
        $connections = toolset_get_related_posts( $post->ID, $atts['relationship'], array(
            'query_by_role' => $origin,
            'role_to_return' => 'other',
            'need_found_rows' => true )
        );
        $count = $connections['found_rows'];
   
    }
   
    return $count;
});

- I've register the above shortcode "get-related-post-count" at:
=> Toolset => Settings => Front-end Content => Third-party shortcode arguments

And used the above shortcode as given under within the "FAQs Directory" view's loop editor section:
=> => hidden link

<div class="row mb-5">
	<wpv-loop>
		  [wpv-conditional if="( '[get-related-post-count relationship='service-faq']' gt '0' )"]
          	<div class="col-md-3">[wpv-post-body view_template="loop-item-in-faqs-directory"]</div>
          [/wpv-conditional]
          
	</wpv-loop>
  </div>

And also modified how you added the child view to "FAQs Directory" view:

[wpv-view name="child-faqs-directory" postid="[wpv-post-id]"]

And within the child view, i've catch the postid shortcode argument as given under:
=> hidden link

<h4>[wpv-post-link item='[wpv-attribute name="postid"]']</h4>

I can see its working as expected. Can you please confirm.

#1821083

My issue is resolved now. Thank you!