Skip Navigation

[Resolved] wpv_filter_query not working with nested view

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

Last updated by vimalS 6 years, 7 months ago.

Assisted by: Beda.

Author
Posts
#629066
Screenshot_3.png
Screenshot_5.png

I have created two views.
1) Courses view (courses post type)
2) Lessons view (lessons post type)
Inside courses view I am calling lesson view and passing course id to lessons view. Many to Many Post relationship is created between courses and lessons post type.
In one of the view I am calling other view and passing post id as shortcode attribute.
When I print query_args it is displaying it for the courses view not for the lessons view.
Please check attached screenshot for the same.

#629118

I am using beta versions of Types,CRED & views

#629298

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Vimal,

Thank you for contacting our support forum.

Can you send me a copy of the filter code so that I can have a look ?

Thanks,
Shane

#629913

Here is my code,

add_filter( 'wpv_filter_query', 'test' );
function test($view_id ) {
    if($view_id == 145)
    {
        echo "nirali"; exit;
    }
    /*global $current_user;
    $types = (array) $query_args['post_type'];
    if ( !is_admin() && in_array( 'company', $types ) ) {
        $query_args['author'] = empty( $current_user->ID ) ? -1 : $current_user->ID;
    }
    return $query_args;*/
}
#630060

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Vimal

Shane is away today (and Monday), let me see if I can help at all.

The code you supplied in your last comment, is that the actual code you are using?

It is mostly commented out and doesn't match the required format, which would be something like:

add_filter( 'wpv_filter_query', 'tssupp_test_function', 101, 3);
function tssupp_test_function( $query_args, $view_settings, $view_id ){

    // Your code here

    return $query_args; // this is required
}

What is it that you are trying to do?

#630460

I have put your code and trying to display view id.

add_filter( 'wpv_filter_query', 'tssupp_test_function', 101, 3);
function tssupp_test_function( $query_args, $view_settings, $view_id ){
    echo $view_id."nirali";
}

But, it is displaying only 2 views ids not the 3rd view id, which I am calling inside 2nd view.

#631026

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Vimal,

Confirming so that I can test this. The ID of the 3rd nested view isn't showing up correct?

Thanks,
Shane

#631296

Hi,

The ID of the 3rd nested view isn't showing up correct?
Ans : Yes

Thanks

#631520

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Vimal,

I can confirm that this issue is replicable.

What I will do is to escalate this ticket for further debugging from our 2nd second tier supporters for further testing,

Thanks,
Shane

#631778

Ok Thanks. Let me know if any update

#631978

Hello Vimal, Beda here.

I was assigned to this task, and I need to interact with you to clarify a few things that remain unclear.

Only if we know how, and why things happen, we can as well provide proper and safe fixes for the issues.

I apologise if this pushes back the ticket by a day or two, but I cannot see what we are supposed to do, and how this is supposed to be a use case, where a BUG becomes visible.

Please let me explain and mention what details I need from you to clarify the issue.

1. I understand you use the Beta Versions of our Plugins (Views updated today again). You have a Many To Many relationships between 2 post types. It is an infinite to infinite Relationship, has no Custom Fields on the Intermediary post Type and that post type is not shown in the admin.
You created a few posts in each type and connected them as per your needs.

Is this correct?

2. Then, I understand you have a View for each Post Type and in each View, you query just one of each Post Types.
This leaves you with a "Parent" and a "Child" View.
In the "Child" View you added a Query Filter that says:

Select posts in Any relationship, that are related to the Post with ID set by the shortcode attribute wpvprchildof.
eg. [wpv-view name="view-name" wpvprchildof="123"]

The loop of this View may output for example the Post Titles.

Is this correct?

3. The "Child" View is now inserted in the Parent View's Loop.
In the ShortCode attribute you pass the ShortCode for the Post ID, so to fetch the ID of the "Parent" post looped in the "Parent View".

This is wrong. You should do that with the Query Filter Setting "Select posts in Any relationship, that are a related to the current post in the loop.", but it will not break anything if you do it the way you chose either.
It is just not the suggested approach for this sort of nesting.

Is this so far correct as well?

If all this is correct, then I cannot replicate any issue because I see all outputs properly and as expected.

But reading on in your ticket, https://toolset.com/forums/topic/wpv_filter_query-not-working-with-nested-view/#post-630460, you mention a 3rd View.

Please, can you explain how many Views you have nested and how?
As well I need to know their Query settings, and more, basically I need to replicate this structure exactly.
I fail to understand where the 3rd View comes from.

After, reading on with the code you provide, Nigel already explains that this code is not correct:
https://toolset.com/forums/topic/wpv_filter_query-not-working-with-nested-view/#post-630060

That code should alter a Query, not echo things.

Can you help me to replicate the issue you are facing, I just need to know what exactly you want to achieve, and how you try to achieve this.

If you want you to spare time I can suggest to provide me with a copy of the site, and the links to where I can see and edit the Views.

This will help me greatly.

Thank you very much for the collaboration.

#632334

OK, thank you so much for these details.

I see you gather all the output here:
hidden link

This page is styled and edited with a Content Template here:
hidden link

In that CT you call the View "[wpv-view name="courses"]", which queries Courses Post Type (related to classrooms and lessons)

This View will query Courses by:

Select posts in a Classrooms Courses relationship, that are related to the Post with ID set by the URL parameter wpv-pr-child-of.
eg. <em><u>hidden link</u></em>

It means, it will either display all results if no such parameter is given, or the ones related to the ID passed in the parameter.
But that View has no Custom Search.
So, no URL parameter is ever passed to it, and it will always display all results.
The View's Loop output confirms that.

At this point it seems you have been working on your site so I was not able to see the rest of the setup:
I see a fatal error on the site:

Fatal error: Call to undefined function gravity_form_enqueue_scripts() in /home/burotijs/public_html/learningplatform/wp-content/themes/toolset-starter-child/functions.php on line 389

(hidden link)

The View's Loop would also calls the View "[wpv-view name="lessons" wpvprchildof="[wpv-post-id]"]", which queries Lessons and selects:

Select posts in a Courses Lessons relationship, that are related to the Post with ID set by the shortcode attribute wpvprchildof.
eg. [wpv-view name="view-name" wpvprchildof="123"]

That's it, I see no third View in this last View or anywhere else.

I see the correct output in the front end (saw, because now a fatal error is there).

I do not see where the issue of "wpv_filter_query not working with a nested view" is applicable.

Please, can you elaborate what I need to look at to see the issue?

I apologise if I miss something.
But up and until now I do not see where the wpv_filter_query breaks here.
In the "Literature" on the front end for example, the correct lesson "PHP Core" appears.
In the Food and agriduaal the lesson1 and lesson4 appear, just as they are connected in the backend.

I do not understand what output is broken, and how the custom code is involved, and where the 3rd new is nested into any of the other views.

#632362

Hi Beda,

Thanks for your help..
Can you please mark my next reply as private?

Thanks

#632368

done.

#632418

Thank you for the details.

Hence:
hidden link

This shows:
Classroom r
Business Economics
Lesson 3

This is all correct data and output as far I see.

There are not 3 nested Views, but two.
One is called in the header and has nothing to do (is not nested within) the others.
It updates the URL parameter to the View can display the correct results, yes, but is not nested.

Additionally, the Views can be filtered through the filter.

Please try:

add_filter( 'wpv_filter_query', 'beda_test', 101, 3 );
function beda_test($query_args, $view_settings, $view_id ) {
	global $current_user;
        
        if($view_id == 103) {//or 145
           if ( !is_admin() ) {
             $query_args['author'] = empty( $current_user->ID ) ? -1 : $current_user->ID;
           }
        return $query_args;
        }
}

Applied to any View it will return no results found because DEV user has no posts created.
Works on both Views if addressed in the code.

I am quickly trying as well such a setup locally again, but until now I see no issues when the filter is applied correctly.