Skip Navigation

[Resolved] Conditional view

This support ticket is created 4 years, 4 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
- 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 6 replies, has 2 voices.

Last updated by Minesh 4 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#1719031

Hello,

I have 2 types: teams and products (woocommerce)
A relation between these 2 types.
I have a view where the teams are displayed.

I would like to set up a condition.
If the team has 5 or less than 5 products, it is not displayed in the view.

thank you.

#1719591

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Before I should work around the offering the solution:
- I would like to know what is the relationship you have between the post type teams and products?
- In your view, are you using pagination or just listing the team names?

#1719649

Hello,

- Many teams and Many product (check Intermediary post type)
- With pagination (Teams ordered by post title, ascending, 99 items per page with manual pagination)

Thank you

#1719697

Minesh
Supporter

Languages: English (English )

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

It seems we need to use View's hook here as you are using pagination.

Can you please share problem URL where you are listing your team 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.

#1721651

Minesh
Supporter

Languages: English (English )

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

I will required FTP access details to add the hook or you can Check file permissions and wp-config.php constants DISALLOW_FILE_EDIT and DISALLOW_FILE_MODS and disable it so that I can add the file using Toolset "Custom Code" section.

I have set the next reply to private which means only you and I have access to it.

#1728181

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've added the following code to your current child theme's functions.php file at bottom of that file:

add_filter( 'wpv_filter_query_post_process', 'func_count_related_parent', 1001, 3 );
function func_count_related_parent( $query, $view_settings, $view_id ){
 
     
    $relationship = 'produit-equipe'; // Edit for relationship slug
	
    if ( $view_id == 2155 && !empty( $query->posts ) ){
		 
		 foreach ($query->posts as $key => $child) {
   
            // get the parent posts of the child  post
           $parents = toolset_get_related_posts(
                $child->ID,
                $relationship,
                array(
                    'query_by_role'     =>  'child',
                    'role_to_return'    =>  'parent'
                )
            );
            // getting total count
           $number_of_parents = count( $parents );

   
            if ( $number_of_parents <= 5 ) {
   
                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;
}

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

I can see now its working as expected. Can you please confirm it works at your end as well.

#1728183

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've added the following code to your current child theme's functions.php file at bottom of that file:

add_filter( 'wpv_filter_query_post_process', 'func_count_related_parent', 1001, 3 );
function func_count_related_parent( $query, $view_settings, $view_id ){
 
     
    $relationship = 'produit-equipe'; // Edit for relationship slug
	
    if ( $view_id == 2155 && !empty( $query->posts ) ){
		 
		 foreach ($query->posts as $key => $child) {
   
            // get the parent posts of the child  post
           $parents = toolset_get_related_posts(
                $child->ID,
                $relationship,
                array(
                    'query_by_role'     =>  'child',
                    'role_to_return'    =>  'parent'
                )
            );
            // getting total count
           $number_of_parents = count( $parents );

   
            if ( $number_of_parents <= 5 ) {
   
                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;
}

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

I can see now its working as expected. Can you please confirm it works at your end as well.