Skip Navigation

[Resolved] How to display a list of all parents and sorting if there is a child yes or no?

This support ticket is created 8 years, 2 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.

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by mcjS 8 years, 2 months ago.

Assisted by: Ross Fisher.

Author
Posts
#364160

I am trying to: Sorting my view (Parent=House) , not on the contents of a parent field , but depending on whether there is or there is not a child (Room) present ?

For example:

House1 Room1
House2
House3 Room3
House4 Room4
House5

The list I want to create I would like to display looks then:

House1 Room1
House3 Room3
House4 Room4
House2 <- has no childeren, so display this one after the parents with children.
House5 <- has no children

I don't know how to do this?

#364172

Thank you for contacting Toolset support, I'd be delighted to assist!

To query if a parent object has children, you would need a custom shortcode such as:

function has_children_of_type( $atts ) {
    global $post;
    extract( shortcode_atts( array(
        'post_type' => 'TYPE', // Make sure to set the correct post type
    ), $atts ) );
    $parent_type = $post->post_type;
    $childargs = array(
        'post_type' => $post_type,
        'numberposts' => -1,
        'meta_query' => array( array( 'key' => '_wpcf_belongs_'.$parent_type.'_id', 'value' => $post->ID ) )
    );
    $child_posts = get_posts( $childargs );
    return count( $child_posts ) == 0 ? false : true; 
}
add_shortcode( 'has_children_of_type', 'has_children_of_type' );

Then go to Views > Settings > Compatibility > Third-party shortcode arguments and add the shortcode.

You can then use it in your views using something like the following:

[wpv-conditional if="( '[has_children_of_type post_type="testimonial" ]' eq '1' )"]
              CONTENT
[/wpv-conditional]

If needed, you can nest views within views as well: https://toolset.com/documentation/user-guides/using-a-child-view-in-a-taxonomy-view-layout/

#365875

Excuses for delay, but now I have other problems with my website.
So I don't have time to follow your advice an testing it.
I will close this thread , read your instructions and when I have questions again, I will make a new thread.

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