Skip Navigation

[Resolved] show view only when filtering

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 7 replies, has 2 voices.

Last updated by Waqar 1 month, 3 weeks ago.

Assisted by: Waqar.

Author
Posts
#2682622
Screenshot table laws.png

Tell us what you are trying to do?

We are offering a legal compliance service to our clients. One of the components of this service is a list or table containing a big number of laws with supplementary information. Some of these lists in different variants, we use for ourselves, they may contain over 2'100 posts or laws respectively. To facilitate the loading of these views, we apply a custom code, so that the page containing the view is first loaded, and then the view is only displayed, if the user selects one of the custom filters. This has become possible based on the following custom script:

<?php
/**
* Zeigt einen View erst, wenn ein Filter ausgewählt ist. Funktioniert für Gesetzesliste. Aus Toolset Hilfe
https://toolset.com/forums/topic/how-to-show-no-initial-results-on-a-view-until-a-filter-is-set/
bezieht sich auf die Gesetzesliste Kunden GaPa
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
function tssupp_no_initial_results( $query_results, $view_settings, $view_id ){

$target_views = array(41691,30836,41764,41749,43134,43140,41739); // Comma-separated list of View IDs

if ( in_array( $view_id, $target_views ) ) {

// if there is a search term set
if ( !isset( $query_results->query['meta_query'] ) && !isset( $query_results->query['tax_query'] ) && !isset( $query_results->query['s'] ) ) {
$query_results->posts = array();
$query_results->post_count = 0;
$query_results->found_posts = 0;
}
}

return $query_results;
}
add_filter( 'wpv_filter_query_post_process', 'tssupp_no_initial_results', 10, 3 );

This works fine for all the views listed with their ID's in the snippet. BUT the last one, ID 41739, is an 'archive organisation', which means: each client has a specific role or organisation taxonomy, called 'organisation'. When the client logges in, he will be able to see HIS SPECIFIC LIST OF LAWS. Each law is assigned to specific clients or organisation. So this archive is a WordPress archive and is not based on a Toolset-query.

At the end we would like to have the same behaviour for this archive as for the 'non-archive pages and views'. But it does not work, the view is loaded even if no filter is selected. Can the snippet be adapted for archives also?

Is there any documentation that you are following?
The above code is coming from the Toolset Support, thank you! The views are constructed with the legacy view.

Is there a similar example that we can see?
no, but I can leave you our credentials, so you can see the details

What is the link to your site?
rechtskonform.ch

#2682814

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

Your observation is correct and the filter 'wpv_filter_query_post_process', works for the views queries, but not for the archives.

For customizing the archives queries, you'll need a more generic filter like the 'pre_get_posts' from WordPress:
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2683973

hi Waqar,

sorry for my late answer, I was on the road... many thanks for your support. So I am not very familiar with WordPress action hooks. I would prefer to use a 'non-archive' view placed on a page, where I could use 'wpv_filter_query' in order to show only the laws which are assigned to the current user. We had a similar discussion in an earlier support case

https://toolset.com/forums/topic/nested-views-with-block-editor/

where you proposed the following code snippet:

add_filter( 'wpv_filter_query', 'filter_include_cat_fn', 1000 , 3 );
function filter_include_cat_fn( $query_args, $view_settings ) {

if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == xxx) ) {
$query_args['tax_query'][0]['taxonomy'] = 'organisation';
$query_args['tax_query'][0]['field'] = 'id';
$query_args['tax_query'][0]['terms'][0] = 456;
$query_args['tax_query'][0]['operator'] = 'IN';
$query_args['tax_query'][0]['include_children'] = '';
$query_args['tax_query']['relation'] = 'AND';
}

return $query_args;
}

In this example you used a static term value '456' in order to programmatically limit the results of the view. But how can I make this filtering dynamic, i.e. only the laws are displayed, which are assigned to the current users role or his organisation?

#2684060

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

I'll need to see how laws are assigned to the current user's role or organization to suggest the necessary changes.

Can you please share temporary admin login details and the link where this archive/view can be seen?

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

#2684528

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Franz,

Thank you for sharing these details.

I'm currently reviewing this setup and will need to perform some tests on my website to suggest the next steps.

Will share the findings, as soon as this testing completes. Thank you for your patience.

regards,
Waqar

#2685223

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Just wanted to let you know that I'm still working on this and will share the findings with you today.

#2685224

Many thanks, I hope this not too much work load for you.....
Franz

#2685624

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for waiting, while I completed my testing and research.

I couldn't find any built-in method through which the 'Advanced Access Manager' plugin connects/links the user roles with the taxonomy terms like the ones in the taxonomy 'Organisationen'.

The only reliable way I can think of to make this setup dynamic is to make sure that the 'name' and not the slugs of both the user roles and the 'Organisationen' terms are the same.

A custom function can be used to get the names of the current user's roles:


function get_current_user_role_names() {

	$current_user_id = get_current_user_id();

	if($current_user_id > 0) {
		global $wp_roles;

		$user_data = get_userdata($current_user_id);
		$user_role_slug = $user_data->roles;

		foreach($user_role_slug as $user_role){
			$user_roles[] = translate_user_role($wp_roles->roles[$user_role]['name']);
		}

		return implode(',', $user_roles);
	}
}

And then in the 'wpv_filter_query' filter code that I shared earlier, you can replace the field attribute with 'name' and the static term ID with names coming from the 'get_current_user_role_names()' function:


add_filter( 'wpv_filter_query', 'filter_include_cat_fn', 1000 , 3 );
function filter_include_cat_fn( $query_args, $view_settings ) {
 
    if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 12345) ) {
        $query_args['tax_query'][0]['taxonomy'] = 'organisation';
        $query_args['tax_query'][0]['field'] = 'name';
        $query_args['tax_query'][0]['terms'][0] = get_current_user_role_names();
        $query_args['tax_query'][0]['operator'] = 'IN';
        $query_args['tax_query'][0]['include_children'] = '';
        $query_args['tax_query']['relation'] = 'AND';
    }
     
    return $query_args;
}

I hope this helps and please let me know if you need further assistance.

Important note: While looking into the user role settings, I accidentally set the default role to 'AI-WTL'. Please be sure to change that to the one that was previously set.

#2685701

Hi Waqar,

this works fine, thank you. However, sometimes the subordinated user filters do not show only the assigend filter selections, but more than that. I will work on that to optimize the filtering.

Many thanks for your support

Franz

franzG-4 confirmed that the issue was resolved on 2024-02-29 07:26:29.
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.