Problem: I would like to give my site visitors the ability to use a checkbox in a custom search View that filters based on whether or not the post has child posts.
Solution: This filter is not built-in to Views, but you can set up a custom field on the parent post type that stores a 1 if child posts exists and stores nothing if no child posts exist. That custom field value can be managed automatically with code.
add_action( 'toolset_association_created', 'toolset_number_child_posts', 10, 5 ); add_action( 'toolset_before_association_delete', 'toolset_number_child_posts', 10, 5 ); function toolset_number_child_posts( $relationship_slug, $parent_id, $child_id, $intermediary_id, $association_uid ) { // add a 1 if child properties exist to facilitate filtering by existence of child post if( get_post_type($parent_id)=='developments' ) { // get most recent property post $property_args = array( 'numberposts' => 1, 'post_type' => 'property', 'orderby' => 'post_date', 'order' => 'DESC', 'toolset_relationships' => array( 'role' => 'child', 'related_to' => $parent_id, 'relationship' => 'developments_property' ), ); $properties_query = new WP_Query( $property_args ); $properties = $properties_query->posts; if(isset($properties[0])) { // set the checkbox custom field value here using update_post_meta update_post_meta( $parent_id, 'wpcf-has-vacant-properties', 1); }else { delete_post_meta( $parent_id, 'wpcf-has-vacant-properties'); } } } function toolset_count_child_properties_on_save( $post_id ) { if( get_post_type( $post_id ) == 'developments' ) { toolset_number_child_posts( 'developments_property', $post_id, null, null, null ); } } add_action( 'save_post', 'toolset_count_child_properties_on_save', 1000);
Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
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 |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 7 replies, has 2 voices.
Last updated by 6 years, 2 months ago.
Assisted by: Christian Cox.