Skip Navigation

[Resolved] wpv_filter_query not triggered on search

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

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/Hong_Kong (GMT+08:00)

This topic contains 8 replies, has 2 voices.

Last updated by dennisC-2 6 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#573100
toolset-initial-view-load.jpg
toolset-incorrect-search-view.jpg
toolset-incorrect-initial-view-load-one-product.jpg

I am trying to: Display purchased products using views and woocommerce

Link to a page where the issue can be seen: hidden link (you need to log in to see this page)

I expected to see: When logged in the page displays a list of products purchased by the user. In order to do this I have used a wpv_filter_query filter. On initial load the results are displayed correctly.

Instead, I got: When you however select one of the search checkboxes to further filter the results, then all available results are shown instead of the correctly filtered result.

In other cases the search is giving filter options which should not be available. eg. I have included an image where the user only own 1 acrylic painting class, but the search filters are allowing the user to filter for oil painting classes (which he doesn't own). If I then select to filter for oil painting classes the acrylic painting class shows.

What this is telling me is that the search / filter is not using my custom wpv_filter_query filter to build the checkboxes or display filtered results.

Can you please look into this for me.

#573106

PS - the wpv_filter_query filter can be found inside the pbcom-shortcodes plugin directory
On line 612 to 624 of the paintbasketcom-shortcodes.php file

#573136

Dear Dennis,

Thanks for the details, I checked it in your website, by modify your PHP codes in file paintbasketcom-shortcodes.php, line 612 to 624 to this:

// add any custum view query filters here
add_filter( 'wpv_filter_query', 'limit_time_on_query', 99, 3 );
function limit_time_on_query($query_args, $view_setting, $view_id) 
{
	// add a custom filter to the My Classes view so we only show the classes bought by the user
	if ($view_id==402)
    {
		print_r($query_args);
		$query_args = array(
            'post_type' => 'product',
            'post__in' => so28362162_get_all_products_ordered_by_user(),
            'posts_per_page' => -1
        );
		print_r($query_args);
    }
   
  return $query_args;
}

And test it in the front-end, I can see it does output the array value of $query_args twice:
Array ( [post_type] => Array ( [0] => product ) [paged] => 1 [suppress_filters] => [ignore_sticky_posts] => 1 [posts_per_page] => 10 [post__not_in] => Array ( [0] => 333 ) [wpv_original_limit] => -1 [wpv_original_offset] => 0 [wpv_original_posts_per_page] => 10 [post_status] => Array ( [0] => publish [1] => private ) )

Array ( [post_type] => product [post__in] => [posts_per_page] => -1 )

The problem is the the custom PHP function so28362162_get_all_products_ordered_by_user() does not return anything, you should ask help from the author of custom PHP function so28362162_get_all_products_ordered_by_user() for it.

#573457

thank you for the rapid response.

I have corrected the error. The problem still however exists.

The user login given is for an admin so they automatically "own all classes".
You can use that login as well as the secondary test login which has a username of:
Whattopaint and the same password as the admin

Admin owns 2 classes
Whattopaint owns 1

In both cases the search is offering all available taxonomies as a filter option
When you click to filter no filtering is happening

#573504
wpv_filter_query.JPG

I tried these in your website, please correct me if there is anything missing:
1) Modify the PHP codes as below:

add_filter( 'wpv_filter_query', 'limit_time_on_query', 99, 3 );
function limit_time_on_query($query_args, $view_setting, $view_id) 
{
	global $user;
	
	// add a custom filter to the My Classes view so we only show the classes bought by the user
	if ($view_id==402)
    {
		echo "<pre>";
		print_r($query_args);
		echo "</pre";

		$additional_args = array(
			'post__in' => get_all_products_ordered_by_user()
		);
		$query_args=array_merge($query_args, $additional_args);
		echo "<pre>";
		print_r($query_args);
		echo "</pre";
    }
   
  return $query_args;
}

2) Login as admin user, edit the view "My Classes", in section "Custom Search Settings", choose option "Full page refresh when visitors click on the search button"
in section "Filter Editor", add a submit button:
[wpv-filter-submit output="bootstrap"

2) Login as user "Whattopaint", test the page again:
hidden link

It see the filter hook "wpv_filter_query" is triggered, and it does output the value of $query_args, see screenshot "wpv_filter_query.JPG", could you point out where I can see the problem:
wpv_filter_query not triggered on search

For the new question:
In both cases the search is offering all available taxonomies as a filter option
When you click to filter no filtering is happening
Please describe detail steps to duplicate same problem, which taxonomy filter should be checked, what is the result you expected to see.

#573506
view-without-filters-selected.jpg
view-with-acrylic-filter-selected.jpg

thank you for the reply.

I have commented out the two print_r() commands in order to show the problem via images. I have added these images to the reply.

To replicate the results log in as Whattopaint and then visit the My Classes page:
hidden link

1) In the image named : view-without-filters.jpg you can see that all possible taxonomy filters are visible.
As the user only owns one class all the options should not show. If you go and edit the product you will see that only these taxonomies have been checked on the product:
Skill Levels: 1 Beginner, 2 Intermediate, 3 Advanced
Medium : Oil Painting
Subjects : Cityscapes, Landscapes
Tutors: Nolan Clark

In other words only these options should be available to select from in the search options as all the rest would return zero results.

2) I now select one of the options. In this case I check the Acrylic Painting box and hit the Submit button
The page refreshes as expected.

As you can see from the image with the file name : view-with-acrylic-filter-selected.jpg
an identical result is returned

As the only class this user owns is not an Acrylic painting class (it is an Oil Painting class as seen in 1) above), no class should show below and a "No classes found" result should be showing.

If everything worked correctly the user should never be shown any options that will return no result as he want to select one of his purchased classes to watch.

#573572

Thanks for the details, I am checking it in your website, will feedback if there is anything found

#573958

I think there is some misunderstandings:
1) Yes, you are right, the Views filter hook "wpv_filter_query" does not take effect on the options of custom search form. And there isn't such a built-in feature within Views plugin, see our document:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
When displaying a View listing posts, this filter is applied to the arguments being generated by the View settings before they are passed to the WP_Query class.

So it can only take effect on the views results, if you agree we can take it as a feature request, our developers will evaluate it.

2) the Views filter hook "wpv_filter_query" does triggered when you display the view "My Classes", see the details I mentioned above:
https://toolset.com/forums/topic/wpv_filter_query-not-triggered-on-search/#post-573504

#573959

thank you for the reply.

1) So what you are saying is the minute I add a wpv_filter_query hook onto the view, it renders the search feature useless.

If that is the case then I would strongly suggest you add this as feature request because what is the point of having one and not the other. Adding this ability should be standard.

2) Yes the wpv_filter_query filter hook does trigger when you first visit the page. As mentioned in the beginning and as confirmed by yourselves it does not when trying the use the search feature.

I will sadly have to use a work around until such time as you have been able to get the wpv_filter_query and the search to work together.

Thank you for your assistance, I highly appreciate the time 🙂

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