Skip Navigation

[Resolved] Views query filters won’t accept changes, keep resetting

This thread is resolved. Here is a description of the problem and solution.

Problem:
Views query filters won't accept changes, keep resetting

Solution:
To prefilter the view with multiple terms belongs to one taxonomy and also offer the taxonomy frontend filter to users in that case you can use the view's filter hook "wpv_filter_query" to filter the views results based on terms given as prefilter to filter the view results as well as offer another taxonomy to filter results using frontend taxonomy filer on frontend.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/views-query-filters-wont-accept-changes-keep-resetting/#post-2260853

Relevant Documentation:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 4 years, 1 month 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 11 replies, has 2 voices.

Last updated by ASCB 4 years ago.

Assisted by: Minesh.

Author
Posts
#2254827
tFyrMQdxiF.png
e0bxadP2Id.png

I am trying to: Set up a query pulling from two different post types, "Posts" and "Meetings", where the posts are of the Toolset custom taxonomy "Videos" or the meetings are of the custom taxonomy "Webinars"

Link to a page where the issue can be seen:
hidden link
password: nuvideo

I expected to see: A list of all posts/meetings that fall under either the taxonomy Videos OR Webinars

Instead, I got: No results from query, and when I reload the view I see all settings have been changed to Functional Category = Video AND Event Category = Webinar AND Webinar Types category is defined by a URL parameter. The latter has never even been part of this query, although I used it in a different one that deals with these two post types months ago. No idea why it'd be pulling conditionals from other Views.

#2255159

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I review the view you are using:
=> hidden link

I can see there is frontend filer added to the view within the "Search and Pagination" section:

<div class="form-sidebar">
  [wpv-filter-start hide="false"]
<h4>Show Me:</h4>
[wpv-filter-controls]
<div class="form-group">
	<label for="wpv-webinar-type"></label>
	[wpv-control-post-taxonomy taxonomy="webinar-type" type="checkboxes" url_param="wpv-webinar-type"]
</div>
  [wpv-filter-submit name="Search" output="bootstrap"]
  <div class="form-group">
	<label for="wpv-category">[wpml-string context="wpv-views"]Categories[/wpml-string]</label>
	[wpv-control-post-taxonomy taxonomy="category" type="checkboxes" url_param="wpv-category"]
</div>[/wpv-filter-controls]
[wpv-filter-end]
  <br><br>  </div>

Can you please tell me what filters you want to keep? Do you need frontend filer with your view?

#2255503

The idea was to pull a list of all articles marked as containing videos (through those two custom taxonomies), then let users sort within those results by a third category describing subject matter. Are frontend and backend filters mutually exclusive?

#2255771

Minesh
Supporter

Languages: English (English )

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

Can you please specify using what taxonomy with what term you want to filter the results and what frontend filters you would like to add so that user can change the filter from the frontend and filter the results. You just name the taxonomy and terms you want to filter with and what frontend filters you want to present to the users.

#2256111
Screenshot 2021-12-31 195414.png

I want it to prefilter for items that have the taxonomies "Videos" or "Webinars" (which are in custom taxonomies, "Functional Categories" and "Event Categories", respectively), and on top of that show users a frontend filter that allows them to limit the results by (WordPress standard) categories.

#2258035

Minesh
Supporter

Languages: English (English )

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

I want it to prefilter for items that have the taxonomies "Videos" or "Webinars":
==>
What prefilter you want to apply here. Do you want to display the posts where both "Videos" AND "Webinars" term is assigned or your want to display posts where "Videos" OR "Webinars" term is assigned?

#2258435

Videos OR Webinars, as shown in the original post.

For the user selection there's six categories we actually care about - Careers; Diversity, Equity, and Inclusion; Science; Publishing; Education; and I Am ASCB - but it looks like the filter will only auto-generate a complete list of every category any queried post has, which is also fine.

#2258659

it appears that the query works, with both frontend and backend filters, if I limit it to just one post type. the error appears when I try to have posts and meetings coexisting in a single query.

#2259985

Minesh
Supporter

Languages: English (English )

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

Can you please tell me what post types you want to include? or you are happy to use one post type?

#2260237

I would like it to incorporate Posts (the WP default ones) and Meetings (a Toolset custom post type)

#2260853

Minesh
Supporter

Languages: English (English )

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

I've created the following test page: hidden link

I've created the following test view: hidden link

Can you please confirm that you see the expected output or you still need any modifications?

I've used the following filter hook to hook in the default categories video and webinars and also used the frontend filter for categories. I've added the code to "Custom Code" section offered by Toolset:
=> hidden link

add_filter( 'wpv_filter_query', 'func_filter_with_multiple_taxonomies', 99, 3 );
function func_filter_with_multiple_taxonomies($query, $setting, $view_id) {
    if($view_id == 129406) {
  
        $query['tax_query'][] = array( 
            'relation'=>'OR',
             array(
            'taxonomy' => 'functional-category',
            'field' => 'term_id',
            'terms' => 4646,
            'operator' => 'IN'
        ),
            array(
            'taxonomy' => 'event-category',
            'field' => 'term_id',
            'terms' => 2893,
            'operator' => 'IN')
          
        );
      
      if(isset($_REQUEST['wpv-category'])){
          $query['tax_query'][] = array( 
           array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => $_REQUEST['wpv-category'],
            'operator' => 'IN'
        ));
      }
   
    }
    return $query;
}
#2263125

Thank you very much! This is a very helpful workaround. I hope for convenience's sake that in the future it's possible to combine filters through the Views interface but this solves my problem, and I have a pretty good idea now how to address it if I need to do something similar in the future.