Skip Navigation

[Resolved] How to build a view filtering only parent posts that have no child posts

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

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by Silvia 5 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1186032

I am building a website that keeps track of all activities in my department.
I want to keep track of the events and use the website as a tool to plan and organise the events, but I also want to record a report of the event once it’s over.
Therefore I built a CPT called Iniziative Organizzate (Planned Events) and a CPT called “Consuntivi” (Final Reports).
Final Reports is child to Planned Events, with a relationship of 1 to 1 (An event can only have one final report).
In the Planned Events I included a field called “STATO” (Status) which can have different values depending on the status of the event (scheduled, cancelled, postponed, on hold, completed). In order to allow users to add a final report to an event, the event itself must have a Status value of “completed”, and since the relationship is 1 to 1, I need to list only the Events that haven’t had a report associated yet.
I want to build a view that lists all the COMPLETED events for which a final report hasn’t been added yet, so users can see at a glance all the events that need to have reports recorded, and it works as a reminder for them as well.

So I would like to know if it’s possible to build a view filtering only parent posts for which there are no child posts of a certain type.
Thank you.

#1186583

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Silvia

It is only possible with custom code, because the count of child posts isn't stored anywhere with the parent post, and you can only filter the parent posts by properties of the parent post.

I've helped people with similar issues recently, so you can adapt the same code.

function tssupp_no_children( $query, $view_settings, $view_id ){
 
    // Edit these
    $my_view_id = 123;
    $relationship_slug = 'relationship-slug';
  

    if ( $view_id == $my_view_id && !empty( $query->posts ) ){
    
        foreach ($query->posts as $key => $parent) {
    
            // get the child posts of the parent post
            $child_posts = toolset_get_related_posts($parent->ID, $relationship_slug, array('query_by_role' => 'parent', 'role_to_return' => 'child', 'need_found_rows' => true));
       
            if ( $child_posts['found_rows'] > 0 ) {
    
                unset( $query->posts[$key] );
                $query->found_posts = $query->found_posts - 1;
                $query->post_count = $query->post_count - 1;
            }
        }
    
        $query->posts = array_values( $query->posts );
    }
    
    return $query;
}
add_filter( 'wpv_filter_query_post_process', 'tssupp_no_children', 101, 3 );

You will need to edit the first lines for the ID of your View and the relationship slug.

Create the View for the parent posts with whatever filters are required (e.g. status is complete). This code will then take the results of that View and strip out any parent posts that already have children, showing only those that do not.

#1186585

Good morning Nigel, and thanks for your help. A couple of questions:
1) $my_child_post_type = 'child-post' = "child-post" is the NAME of the cpt, not the slug, right?
2) changing the parameters and naming the function in a different way I can use this code with all the cpt that need the same process, am I correct?

#1186595

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sorry, I meant to delete that line defining the child post type because it is not needed in this specific solution as it is implied by the relationship. (I'm editing my previous reply to delete that line.)

Yes you can re-use this with other post types. Because you have to define the relationship slug and this would be different in each case, you should copy this code snippet as it is and change the name of the function ("tssupp_no_children") which appears twice, on the first and last lines of that code snippet.

#1186599

Awesome, Nigel, thank you so much!
The code works perfectly and I learned something new.
Thank you again, have a good day!

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