Skip Navigation

[Resolved] Filtering a View by category on frontend when the Query Filter is also category.

This support ticket is created 3 years, 5 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
- 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 Luigi Bella 3 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#2136151
Screen Shot 2021-08-06 at 8.41.18 PM.png
Screen Shot 2021-08-06 at 8.41.02 PM.png

Tell us what you are trying to do? I am trying to display items from my media library with the category 'NDBPA' then also have a frontend filter(dropdown) that will check if in addition to 'NDPBA', another category is selected.

What is the link to your site? hidden link

I can't select Categories as a Filter option because I'm already using it to define the Query Filter.
The problem is I need to add a frontend filter that checks if another category is also selected, such as 'Dominican Republic' or 'El Salvador' in addition to the 'NDPBA' category that I'm using to define which items shuld be loaded before filtering.

#2137771

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Do you mean that you want to add a frontend drop-down filter using which user should be able to select the option and based on selected option the view should be displayed filtered results and you also want to pre-filter the results using few other terms as well?

For example: there are terms a,b,c,d,e,f and g

And you want to prefilter with terms a+b and the term selected by user (lets say d) from frontend to the filtered results should display the posts belongs to terms a+b+d?

#2138175

Yes, that's correct.

If there are terms a,b,c,d,e,f and g
I want a frontend dropdown filter where user can filter terms, let's say they can choose between D,E,F. I want to make sure that what's displayed before frontend filtering and after is limited to what's in terms A OR B.
In this case, terms C and G are not available in the View at all.

#2138651

Minesh
Supporter

Languages: English (English )

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

Can you please send me problem URL where you want to display the view and setup the content accordingly and share one example that if I filter with X term what result you want to display and with what terms you want to pre-filter the result.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2138983

Minesh
Supporter

Languages: English (English )

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

I need bit more information.

Do you mean that when we load the page that holds the view:
- hidden link

It should initially display only posts belongs to term (categories): NDPBA, Final Report, and PDC Fact Sheet?

So when view displays first - it should only display posts belongs to the following categories: NDPBA, Final Report, and PDC Fact Sheet?

After that, you also want to offer user the category filter where user should be able to filter by selected category - right?
If yes:
At that time, do we need to consider the NDPBA, Final Report, and PDC Fact Sheet categories or only the selected category?

#2139735

Hi Minesh,

That's correct, It should initially display only posts belongs to term (categories): NDPBA, Final Report, and PDC Fact Sheet.

Then there will be a dropdown filter available for users to select if they want to narrow down the results (NDPBA, Final Report, and PDC Fact Sheet) to results that ALSO have one the following terms (categories): All(default), Argentina, Dominican Republic, El Salvador, Guatemala, Honduras, Jamaica, Myanmar, Nicaragua, Panama, Paraguay, Peru, Sierra Leone, and Trinidad and Tobago.

To clarify, results of the View must contain one of the following categories: NDPBA, Final Report, and PDC Fact Sheet. These values are not to be displayed again in the dropdown list. The dropdown list will check if results ALSO contain one of the following categories: All(default), Argentina, Dominican Republic, El Salvador, Guatemala, Honduras, Jamaica, Myanmar, Nicaragua, Panama, Paraguay, Peru, Sierra Leone, and Trinidad and Tobago.

#2140261

Minesh
Supporter

Languages: English (English )

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

To clarify, results of the View must contain one of the following categories: NDPBA, Final Report, and PDC Fact Sheet. These values are not to be displayed again in the dropdown list. The dropdown list will check if results ALSO contain one of the following categories: All(default), Argentina, Dominican Republic, El Salvador, Guatemala, Honduras, Jamaica, Myanmar, Nicaragua, Panama, Paraguay, Peru, Sierra Leone, and Trinidad and Tobago.
==>
So the end result when I select one category from dropdown lets say Argentina.

Then you want to list all posts where category NDPBA, Final Report, and PDC Fact Sheet is selected AND Argentina or
NDPBA, Final Report, and PDC Fact Sheet is selected OR Argentina?

#2140451

If you select Argentina from the dropdown, I need to list all posts where category NDPBA OR Final Report OR PDC Fact Sheet is selected AND Argentina.

What's being filtered is still limited to the original results containing NDPBA OR Final Report OR PDC Fact Sheet, AND must include the category selected from the dropdown.

#2140943

Minesh
Supporter

Languages: English (English )

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

I've added the following code to "Custom Code" section offered by Toolset with "Toolset custom code" code snippet:
=> hidden link

// Filter view results 
add_filter('wpv_filter_query','func_filter_view_by_tax',10,3);

function func_filter_view_by_tax($query_args, $settings, $view_id){
    if($view_id == 3284318 and ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX )){
      
      $query_args['tax_query'][] = array(
            'taxonomy' => 'media_category', // taxonomy name
            'field' => 'slug',
            'terms' => array( 'ndpba', 'final-report','pdc-fact-sheet'), 
            'operator' => 'IN'
        );
     } else if($view_id == 3284318 and (defined( 'DOING_AJAX' ) || DOING_AJAX )){
        
        $query_args['tax_query'][] = array(
            'taxonomy' => 'media_category', // taxonomy name
            'field' => 'slug',
            'terms' => array( 'ndpba', 'final-report','pdc-fact-sheet'), 
            'operator' => 'IN'
        );
      
     }
    return $query_args;
}

// Remove 'ndpba', 'final-report','pdc-fact-sheet' options from view's filter dropdown
function tssupp_restrict_terms( $terms, $taxonomies, $args, $term_query ){
 global $post;
  
  
    // 1. pages where form (or search View) is inserted
    $pages = array(3284038);

    // 2. which taxonomy (slug)
    $taxonomy = 'media_category';

    // 3. add terms to blacklist *OR* whitelist, not both
    $blacklist = array('ndpba', 'final-report','pdc-fact-sheet');
    $whitelist = array();

    if ( is_page( $pages ) && $taxonomies[0] == $taxonomy ) {

        if ( !empty( $blacklist ) ) {

            foreach( $terms as $key => $term ){
     
                if ( in_array( $term->slug, $blacklist ) ) {
                    unset($terms[$key]);
                }
            }
        } elseif ( !empty( $whitelist ) ) {

            foreach( $terms as $key => $term ){
     
                if ( !in_array( $term->slug, $whitelist ) ) {
                    unset($terms[$key]);
                }
            }
        }
    }
     
    return $terms;
}
add_filter( 'get_terms', 'tssupp_restrict_terms', 10, 4 );

In addition to that, you will have to compromise bit here as the shortcode [pdf-embedder url='[wpv-post-url]'] does not rendered when we are using AJAX filter so as of now I've change the view's search setting to display the result when you click on Submit button and added the submit button.

#2140999

Minesh, you are absolutely brilliant!
Thank you so much for your help.

I had an issue on pages other than the first page of results but changing the settings from AJAX Pagination to Reload Page fixed it.

The last thing I'm trying to figure out is how I can set the output conditionally to be [pdf-embedder url='[wpv-post-url]'] if the post-url is a pdf (or contains .pdf in the url), and to display the image if it's not a pdf.

Here is an example of the issue: hidden link

#2141003

Minesh
Supporter

Languages: English (English )

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

That is another issue, as per our support policy we entertain only one question per ticket. This will help other users searching on the forum as well as help us to write correct problem resolution summery.

Also, I've changed and adjusted the following filter code as given under as we removed the ajax filtering.

add_filter('wpv_filter_query','func_filter_view_by_tax',10,3);

function func_filter_view_by_tax($query_args, $settings, $view_id){
    if($view_id == 3284318 and ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX )){
      
      $query_args['tax_query'][] = array(
            'taxonomy' => 'media_category', // taxonomy name
            'field' => 'slug',
            'terms' => array( 'ndpba', 'final-report','pdc-fact-sheet'), 
            'operator' => 'IN'
        );
     } 
    return $query_args;
}

I'll split the ticket with your new question and handle it with split ticket. Glad that you are really happy with the solution I shared. Feel free to share your feedback 🙂 and mark this ticket resolved.

#2141367

My issue is resolved now. Thank you!