Skip Navigation

[Closed] WP_Query action hook on AJAX requests

This support ticket is created 7 years, 6 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.

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
- 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+01:00)

Tagged: 

This topic contains 25 replies, has 3 voices.

Last updated by Nigel 7 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#449979

Hi Nigel \ Toolset...

Any further progress on this?

Looking back, it still does not make sense that using AJAX does not filter posts using WP_Query and it seems identical to the previous issue I was having with Access where the errata mentioned solved the problem.

Also, I am in the process of changing from using Archive Pages to using pages with Views - but I'm hoping we can find 1 solution that can be applied to every CPT selected.

It seems we were very close to having a solution?

Thanks

#450142

Nigel
Supporter

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

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

Hi Tristian

I have been juggling several demanding tickets and trying to divide my time equally between them.

I am just returning to this now and I'll post an update when I have gone through it again.

#450187

Nigel
Supporter

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

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

Screen Shot 2016-10-25 at 14.50.56.png

Hi Tristian

OK. If you are happy to use pages with Views added rather than an archive page then I think we have an alternative solution that doesn't require you to customise WP_Query which means we don't have to worry about what hook can be used on ajax-based requests.

So you have the View for your containers.

We want to add a filter so that only containers belonging to the same company as the currently logged-in user are shown.

On the page where you insert the view shortcode you can pass a shortcode argument for the parent company which is the user meta field of the logged-in user.

I set up a test and gave the user field a slug of company-id.

So, I insert my "filtered-containers" view via a shortcode with the following arguments:

[wpv-view name="filtered-containers" wpvprchildof="[types usermeta='company-id' user_current='true'][/types]"]

Now, I add a filter to the filtered-containers view which uses that shortcode argument in a post relationship filter. See the screenshot.

I tested this and it worked as expected. I didn't test on a custom search view with parametric filters updating via ajax, but there is no reason to think that will not work, too.

The one gotcha is that if the user is not logged in they may see all results.

You can use conditional shortcodes here (https://toolset.com/documentation/views-shortcodes/#wpv-conditional) to either not insert the view with the search form at all for guest users and display something else instead, or for those users insert the same view but with a non-functioning company id, such as '1', hard-coded as the shortcode argument so that they never get any matching results.

I think that should do it, please let me know either way.

#450550

Hi Nigel,

Thanks for your continued help on this...

Unfortunately your proposed solution isn't going to work in my scenario for many reasons. I am trying find a centralised solution that doesn't involve updating countless views. I believe you can only have 1 relationship filter per view and this already isn't enough in some places... this is also just for containers, but I also have other filtering with CPT's to do where I know defining this relationship on a view by view basis won't work.

I do feel we are fundamentally dealing with a bug here and that we have also been very close to making this work. I don't see any way why updating a view with a URL parameter on page load will return different results to filtering with the same parameter and updating with AJAX.

It appears that filtering through WP_Query is technically the right solution I am after and I was experiencing the same issue with the Access plugin when filtering using AJAX that was fixed in the errata mentioned which suggests a known bug or there is a hook or a way to make this work.

I have tried to use 'pages' instead of 'archives' and my developer couldn't find the 'fake' archive loop mentioned at all.

How can we make AJAX work in the way it's supposed to?

Is there another hook we can try that applies to all posts filtered by AJAX - we have already establishing that AJAX is reloading posts and just need to apply the same filter.

Also, Is it possible to ask the developer of the errata which may offer some clues?

Many thanks.

#450606

Nigel
Supporter

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

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

Hi Tristian

I've started over with some simple testing to clarify one or two things that will help identify whether this is a bug or not. A bug is a feature that doesn't work, which is not the same thing as a missing feature.

So, firstly, the question of whether the Views api filters work when results are updated via ajax or only on page refreshes.

I tested wpv_filter_query and the answer is that it works both on page refresh and on ajax updates.

Here is my ever-so-simple test code added to my functions.php file:

/** 
 * Test query filters
 */
function test_query_filters( $query_args, $view_settings, $view_id ){

	error_log("### testing query filters ###");

	if ( $view_id == 14 ) {
		error_log("@@@ view id 14 @@@");
	}


	return $query_args;
}
add_filter( 'wpv_filter_query', 'test_query_filters', 101, 3 );

And here is my debug log which I have annotated for clarity:

// This is the initial page load
[26-Oct-2016 13:39:55 UTC] ### testing query filters ###
[26-Oct-2016 13:39:55 UTC] @@@ view id 14 @@@
[26-Oct-2016 13:39:55 UTC] ### testing query filters ###
[26-Oct-2016 13:39:55 UTC] @@@ view id 14 @@@

// And this is when an ajax update was triggered shortly after
[26-Oct-2016 13:40:14 UTC] ### testing query filters ###
[26-Oct-2016 13:40:14 UTC] @@@ view id 14 @@@
[26-Oct-2016 13:40:14 UTC] ### testing query filters ###
[26-Oct-2016 13:40:14 UTC] @@@ view id 14 @@@

So, from that I conclude that views api filters work both on page refreshes and on ajax updates.

If you are able to add your views to pages (rather than using WordPress archives) then you should be able to hook into the query and modify it as required and have it work regardless of whether the updates occur via ajax or not.

I looked at your custom plugin but I wasn't able to see how you are hooking into the Views filters, you may need to review that.

Now, can we filter the query when using WordPress archive pages?

I don't know. I re-tested the solution I proposed in https://toolset.com/forums/topic/wp_query-action-hook-on-ajax-requests/#post-448746 and it continues to work on my test site, but there may be a better solution.

I have put in a question to the relevant developer about whether there are any undocumented hooks that can be used, and whether they should be expected to work via ajax or not, and I will update you when I hear back from him.

#450633

Nigel
Supporter

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

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

Hi Tristian

I heard back from the developer who says that there is an action hook (not a filter) named wpv_action_apply_archive_query_settings, and it will work both on page load and ajax updates.

It is not documented so your developer should inspect the source code to see the available arguments and how to update them.

It comes with a health warning though (as does using the documented Views API hooks), which is that if you modify the meta_query parameter you run the risk of breaking the other filters which a view or archive might add. Your code will need to be smart enough to prevent that happening.

One tip in the case of adding a condition that posts have a parent with a company id equal to that set on the current user meta field is that you might add such a query filter to your view or archive with a placeholder value for the company, and then your custom code that hooks into the query simply has to swap out the placeholder value rather than build the meta_query (which may have conditions from other filters) from scratch.

There is not much else I can offer, I hope your developer has more success using that hook.

#451271

Hi Nigel,

Thanks for the additional info - my developer is away for a few days but will update next week.

Thanks,

Tristian

#451386

Nigel
Supporter

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

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

OK, good luck, and let me know either way what happens.

#452252

I received an automated email to close this ticket... my developer has been away for a few days, so yet to see if this resolves things... will update shortly. Thanks

#452966

Minesh
Supporter

Languages: English (English )

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

Nigel is on holidays and he will be back on next Monday. He will get in touch with you as soon as possible once he will back to work.

#454110

Nigel
Supporter

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

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

Hi Tristian

Just noting that I am back from a couple of days off and posting here to keep the thread alive in case you need to follow-up with anything when your developer returns.

The topic ‘[Closed] WP_Query action hook on AJAX requests’ is closed to new replies.