Sauter la navigation

[Résolu] Filter view: houses with 2 rooms

This support ticket is created Il y a 6 années et 7 mois. 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)

Marqué : 

Ce sujet contient 3 réponses, a 2 voix.

Dernière mise à jour par Nigel Il y a 6 années et 7 mois.

Assisté par: Nigel.

Auteur
Publications
#882883

Hello,

I have Parent CPT 'house' and its child CPT 'room'.

I want to display a view with all the houses with 2 rooms.

Thanks!

#883227

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Hi Jesus

You want a View that displays houses, but only those houses with 2 room child posts, not more, not less.

It's not possible to produce a query with Views that would only return house posts with 2 room child posts.

What you would need to do is to create a View which returns all house posts and then use the wpv_filter_query_post_process API filter to modify the results, as described here: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

You would need to iterate over the found posts using a foreach loop and for each house post you would get its room child posts, for which you can use the function types_child_posts like so:

$rooms = types_child_posts( 'room', array( 'post_id', $post_id ) );

where $post_id comes from the house posts you are iterating over.

For house posts that do not have 2 child room posts you can remove the post from the results, being careful to update the post_count and found_posts parameters at the same time.

If you try and get stuck let me know and I will see if I can help.

#887727

I'm sorry, but I can't understand what may I do...

#889231

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Hi Jesus

You'll need to add some custom code using the API to your site. I have provided a sample of the code you would need to add (to your theme's functions.php file, or using a plugin such as Code Snippets) below, but please note that providing or supporting custom code is outside our support policy, and if you need further help you should consider contacting a developer such as those listed on our contractors page: https://toolset.com/contractors/

function tssupp_two_rooms( $query, $view_settings, $view_id ){

	$house_slug = 'house';  // Edit the house post type slug
	$room_slug = 'room';  // Edit the room post type slug

	if ( $view_id == 25 ) {  // Edit ID

        $houses = $query->posts;
  
        foreach ($houses as $key => $house) {
  
        	// Get the child rooms of this house
        	$rooms = get_posts( array( 'post_type' => $room_slug, 'meta_key' => '_wpcf_belongs_' . $house_slug . '_id', 'meta_value' => $house->ID, 'numberposts' => -1 ) );

            if ( count($rooms) != 2 ) {
  
                // remove from results because does not have 2 rooms
                unset( $houses[ $key ] );
            }
        }
   
        $number_of_posts = count( $houses );
 
        $query->post_count = $number_of_posts;
        $query->found_posts = $number_of_posts;
 
        // reindex and update results
        $query->posts = array_values( $houses );
    }

	return $query;
}
add_filter( 'wpv_filter_query_post_process', 'tssupp_two_rooms', 101, 3 );

Note that you have to edit the ID of the View, and the slugs of the house and room custom post types.