Skip Navigation

[Résolu] Control View’s output with Access

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
Is it possible to create a View that respects all Toolset Access rules set for a post type, even groups and single post settings of access?

Solution:
No, not without complex custom code.
A Feature is filed to allow this.
Please add your voice by opening a new ticket for it.

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

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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 43 réponses, has 5 voix.

Last updated by Beda Il y a 6 années et 4 mois.

Assisted by: Beda.

Auteur
Publications
#518887

Hi Beda... Thank you for the response. Well there seems to be a few glitches with this approach or perhaps some complications that require further refinement or specificity.

First the end result is backwards from what I am looking for. The goal is for the View to display all Project posts except for those that have the Access Post Group applied to them when a user with the user role of Subscriber is logged in. The Access Post Group has "Read" ticked off for Subscriber.

Second it seems I have to increase the number from 99 to 101 for the above backwards behavior to start working for Subscriber, see code below. If I leave the number at 99 the view just displays "No Items found". FYI, I don't really know what this does just figured it had something to do with how the code loaded, hierarchy and such.

add_filter( 'wpv_filter_query', 'prefix_show_only_current_author', 101, 2 );

Third the Views custom search "Sort Projects" dropdown dismisses the functionality of your provided solution once used. Once I select something the Posts become visible again. Just as a reminder, the search uses AJAX to load the results when a selection is made.

Lastly I have a Dashboard page that has a completely separate view with another nested view for displaying brother post titles for the Projects custom post type and it completely breaks when using the filter code you provided. If I leave the number at 99 from my Second point above, it actually displays all my blog posts and if I increase the number to 101 it just displays "No Items found".

Just a reminder, I am not able to give FTP access to the site. I hope all this makes sense, did my best to describe things but as mentioned I am a little out of my depth when it comes to the nuances of this code.

Kind Regards,
Dave

#518966

Minesh
Supporter

Languages: Anglais (English )

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

Beda is on holiday and will be back on Wednesday. As Beda is guiding you from your first reply I will let Beda continue with this ticket and guide you with your queries.

#519149

Hi Dave,

I'm Mohammed, the Toolset support team leader.

I'm sorry if there is any inconvenience in the previous thread.

As you knew, Beda is a second tier supporter and is working with you on the issue. but, he will be back from his vacation on the next Wednesday.

Minesh also is one of the most experienced supporters in the team and I'm sure that he is able to support you and he is available tomorrow.

Please let me know if you want Minesh to continue with you or you prefer to wait for Beda to be back.

Please elt me know your decision and mention my name in you reply so that I can follow up with the ticket.

Thanks.

#519167

Hello @mohammed... Thank you for taking the time to reach out and express your concern regarding my request for support. Also thank you for offering Minesh for earlier support but it would probably be best to let Beda bring this to completion as he and I seem to be on the same page. Having said that, I defer to your expertise and guidance regarding this matter.

Again thank you and the overall team for all that you do and your dedication to providing great support, even when things become difficult. I hope the functionality discussed in these threads makes its way into the Toolset system as it is much needed.

Kind Regards,
Dave

#519672

Hello Dave, sorry, I was off for the Labour Day.

1. The result is backwards from what I am looking for. The goal is for the View to display all Project posts except for those that have the Access Post Group applied to them when a user with the user role of Subscriber is logged in.

This is not the issue. It can be done simply in the code by changing the default behaviour of the meta_query.

$query_args['meta_compare'] = '!='

2. it seems I have to increase the number from 99 to 101

This is the priority with which the code runs.
By default, Views hooks can be used from 1 to 100.
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

It just means there is another code in your Site running at 99, making it impossible for our Hook to interfere.

3. the Views Custom search "Sort Projects" drop-down dismisses the functionality of your provided solution once used. Once I select something, the Posts become visible again. Just as a reminder, the search uses AJAX to load the results when a selection is made.

I did not know this is also with a Custom Filter.
That is the reason a backup of the site, and a local install would have avoided all this back and forth.

You can try to hook in later, with https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process as an example.

About your last point, if you apply this only to the given View it will never interfere with other Views.
If this View is used elsewhere, of course, that query will be affected as well.

I need a Site's Snapshot; there is no way around this.

Firstly, this is all Custom Code that we do not assist in support.
It uses our API, that is why we assist the How to and give tips and assistance, and it is a lacking feature in Access, which is why I go beyond the rules and will try to craft a solution for you.

But, this is not a simple, fresh install with just one task and that's it.
It seems to be a complete website with lots of interfering things.
I need to develop a code and test it. I cannot provide here some half working patches and then try to debug them based on nothing.

Can you provide me with a Snapshot?
I added the info on how to do that earlier.

This will help greatly.
Please also add the links to the page where I see the issue and where it's built.

Thank you very much for understanding and the patience.

#520090
Bildschirmfoto 2017-05-04 um 14.33.39.png

The Post Debug section is intended to be used exactly for that:
Debug Information.

Everything else is not accepted there.

As such, I received an empty submit (see above screenshot)

If you just want to share a link for me, there is no need to use the special Fields.
You can just paste it in the Reply Editor and submit it to me.

Please also don't forget to add the proper edit screen and view screen links.

I will now inform my colleagues that they have to intervene here, as I am off tomorrow and back only Saturday.
They will eventually take this thread to provide you with a solution faster

Related to that other thread, I debugged that.
It's not an issue. It's a misunderstanding of Access usage.
Christian will update you with all information and how to today.
I make sure this will happen.

Thank you for your patience.

#520862

I am sorry but there is a Server Login and I can by my best will not find the login details to that one.
I attached a screenshot.

None of the provided login details do work for that one.

#521102

This code below will display all posts where the Field does not exist (as such, fulfills your need):

add_filter( 'wpv_filter_query', 'prefix_show_only_current_author', 101, 2 );
   
function prefix_show_only_current_author( $query_args, $view_id ) {
    //Get the current user object
if ($view_id['view_id'] == 257) {
    $current_user = wp_get_current_user(); 
    //If it's an admin (or any other role)
    if ( in_array( 'subscriber', (array) $current_user->roles ) ) {//Change here your user role
               
            //If it's post type "post" and is not in the admin backend
        $types = (array) $query_args['post_type'];
        if ( !is_admin() && in_array( 'project', $types ) ) {//Change here your Post Type
           
                //Set the new Query Arguments
            $query_args['meta_key'] = '_wpcf_access_group';//Insert here the Custom Field Key

            $query_args['meta_compare'] = 'NOT EXISTS';
        }
           
    }
        //Return this Query Arguments.
    return $query_args;
      
    }
}

The problem is now, with the AJAX search this does not work. The PHP is lost after AJAX performance.

I am not sure whether this is a BUG or expected, I will query with the Developers in regard.
What you can do is use Full Page reload instead, that works.
I'd say this is expected because on AJAX we do not call the PHP File.

For now, you can use that Code.
I will check what we can do related to the AJAX problem.

You could use this for the AJAX problem, but this will knock out the AJAX search instead, as we hook in AFTER the query is run 🙁

add_filter( 'wpv_filter_query_post_process', 'prefix_show_only_current_author', 101, 3 );
   
function prefix_show_only_current_author( $query, $view_id, $view_settings ) {
    //Get the current user object

    $current_user = wp_get_current_user(); 
    //If it's an admin (or any other role)
$args = array(
	'post_type' => 'project',
	'meta_value' => 'wpcf-custom-group-5ac1c0cfbe49b5a0f25945e2d2e3b0ac',
	'meta_key' => '_wpcf_access_group',
	'meta_compare' => 'NOT EXISTS'
);
$query = new WP_Query( $args );

               
            return $query;
}

So. We do have the only possible solution for this above.
The remaining issue wbout the AJAX, will eventually end in a new feature, I will keep you updated.

#521517

Doh, this is my bad.

Please use the first code above, but remove the !is_admin() && part.

This checks if we are not in admin, but AJAX requires admin so it returns is_admin == true, which means, our filter dies as soon an AJAX request happens.

If you remove that, it works, and it will not affect the backend because you say "if ($view_id['view_id'] == 257)" and that does it all.

Please let me know if this is unclear.

#521826
Child post view.png

Hi Beda... Had a go with the code and it seems to work on the Views Test page. As for the Dashboard page which has a parent view "Project List View" that displays the CPT Project Groups and a child view "Child View Projects" that is intended for the displaying of brother projects, nothing shows up. I thought to ad the "or" php operator with the "Child View Projects" ID 345 and that seems to get things working. While this is positive, I want to confirm a couple things and get the Access Post Group working properly on the Dashboard page. Below are a few more questions.

1. Is what I am doing in the below code the correct way to include two or more ID's or is their a more efficient way to do it?

2. I noticed we are no longer using the meta_value "wpcf-custom-group-5ac1c0cfbe49b5a0f25945e2d2e3b0ac" for the Access Post Group. While everything seems to work, why is this no longer needed?

3. After adding the additional ID and I log in as Subscriber, the Dashboard page does not display the projects that have the Access Post Group applied which is good, but it does display " No items Found". The desired behavior would be for the Dashboard page to not display either the "Project List View" as well as not display the "Child View Projects" view when there are no child projects found as the are restricted via Access Post Group. See screenshot for more detail.

/* Custom View query based on Access Post Group */
add_filter( 'wpv_filter_query', 'sda_access_post_group_view', 101, 2 ); 
function sda_access_post_group_view( $query_args, $view_id ) {
if ($view_id['view_id'] == 257 or 345) {
    $current_user = wp_get_current_user();
    if ( in_array( 'subscriber', (array) $current_user->roles ) ) {
        $types = (array) $query_args['post_type'];
        if ( in_array( 'project', $types ) ) {
            $query_args['meta_key'] = '_wpcf_access_group';
            $query_args['meta_compare'] = 'NOT EXISTS';
        }     
    }
    return $query_args;
    }
}

Kind Regards,
Dave

#522426

1. Is what I am doing in the below code the correct way to include two or more ID's or is their a more efficient way to do it?

That's OK but you can also do this:

if (in_array($view_id, array( "3910", "1992", "1927", "673", "1994", "2000", "2004", "2008", "3198", "3197", "3196", "3195", "3463", "3535", "etc" ) ) )  {

2. I noticed we are no longer using the meta_value "wpcf-custom-group-5ac1c0cfbe49b5a0f25945e2d2e3b0ac" for the Access Post Group. While everything seems to work, why is this no longer needed?

You did let me know, you do not need to show those posts.
There is, therefore, no point in checking the value, since you simply want posts where the access group is not applied (means, there is no _wpcf_access_group field at all)
You could also check for posts where the value is not equal to that access group, of course.
It's the same, basically, it depends whether you will have several more groups and some of them should be displayed, then you should compare the value.

3. For this, you need to check the Child View and check it's output, and conditionally hide the View from the Parent View.
This could be done with get_view_query_results() and using an HTML condition to check if that returns 0 posts or some posts.
You can use this DOC as reference:
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

Assistance for this issue will be given in a new ticket, as it is a different approach and issue.

#523752

Hello Beda... Thanks for the explanations and info. In regards to item three would value if you could provide me with the code that would be used as this is a little out of my depth. All this pertains to the original issue which is to get Access Post Groups to actually secure the page throughout the entire site but if you feel you need to provide this info in a new thread, please do so and provide me with the link.

Just as a reminder item three is in reference to the Dashboard page that has the nested view configuration for displaying brother posts. You have the working site and full access to it.

Kind Regards,
Dave

#523885

I just went over your last post again, and saw this:

1. After adding the additional ID and I log in as Subscriber, the Dashboard page does not display the projects that have the Access Post Group applied which is good, but it does display " No items Found".

You can simply delete that from the View in question.
It's a simple string, just below your Loop.
Once deleted that won't show up anymore.

2. This code below checks if the View with ID 5 has some results. If not, it returns 0.
that can be used in an HTML conditional the conditionally show or hide the View.
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

function is_there_a_post() {
	$filtered_posts = get_view_query_results( 5 );
	if ($filtered_posts[0] == 0) {
		return 0;
	}
}
add_shortcode('is_there_a_post', 'is_there_a_post');

This is custom code, and if you stumble over issues, we would kindly ask you to open a new ticket for the issue, which we can then assist with the limited possibilities of Custom Code:
https://toolset.com/toolset-support-policy/

Thank you for your patience.

#526016
Dont display if no children.png

Hey There Beda... Thanks for the "No items found" tip. As for the conditional and the function this does not seem to achieve the desired result. I provided a screenshot of the area, Parent and child that should not be displayed when logged in as Subscriber.

As for the link you provided it seems to be in reference to conditionals using functions. Also it appears your code is intended to use a short code in the conditional. Regardless this technique seems to either hide all the posts or none at all. Would value if you could clear this up and provide a little more detail.

#526442

The code I shared does check if the View passed as ID in it has some results.
If not, it won't display.

You can use this as a function or as a shortcode.
Here is the link to the conditional HTML as ShortCode, I am sorry previously I linked the functions one, it's on the same page:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/ > Checking Custom shortcodes

You can test on your site like this:

1. var_dump() the get_view_query_results() function
hidden link
2. This will show you what it returns when there are posts found and when there are none
3. To that value, you have to compare to.

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