Skip Navigation

[Resolved] Query arguments prevents

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

Problem:

I am using custom code to modify a View’s query based on taxonomy term slugs. This works, but now the taxonomy search filters (ld_course_category and ld_course_tag) no longer function properly in the view.

Solution:

The issue arises because your custom tax_query overrides the existing query, including filters for ld_course_category and ld_course_tag. To fix this, merge your custom tax_query with any existing tax_query conditions to preserve the original filters. Use a check to append your custom taxonomy query to the existing tax_query instead of replacing it.

Relevant Documentation:

https://toolset.com/documentation/legacy-features/views-plugin/passing-arguments-to-views/

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.

This topic contains 1 reply, has 2 voices.

Last updated by Christopher Amirian 2 months, 1 week ago.

Assisted by: Christopher Amirian.

Author
Posts
#2744980

We're using the following code to modify a View's query based on a taxonomy term slug. This works great, but unfortunately, now my taxonomy search filters (ld_course_category and ld_course_tag) in the view do not work anymore. What are we doing wrong?

add_filter( 'wpv_filter_query', 'filter_toolkit_course_enrolled_user_toolkit', 102, 3 );
function filter_toolkit_course_enrolled_user_toolkit( $query_args, $view_settings, $view_id ){

$view_ids = array(7979,8118);

if (isset($view_settings['view_id']) && in_array($view_settings['view_id'], $view_ids )) {

$user_id = get_current_user_id();
$kostenstelle = get_user_meta($user_id , 'wpcf-std-kostenstelle', true);
$qualifikation = get_user_meta($user_id , 'wpcf-std-qualifikation', true);
$bereich = get_user_meta($user_id , 'wpcf-std-bereich', true);
$funktion = get_user_meta($user_id , 'wpcf-std-funktion', true);
$betriebsteil = get_user_meta($user_id , 'wpcf-std-betriebsteil', true);

$query_args['tax_query'] = array(
'relation' => 'AND',
array(
'taxonomy' => 'tr-kostenstelle',
'field' => 'slug',
'terms' => array('Alle', $kostenstelle),
),
array(
'taxonomy' => 'tr-qualifikation',
'field' => 'name',
'terms' => array('Alle', $qualifikation),
),
array(
'taxonomy' => 'tr-bereich',
'field' => 'name',
'terms' => array('Alle', $bereich),
),
array(
'taxonomy' => 'tr-funktion',
'field' => 'name',
'terms' => array('Alle', $funktion),
),
array(
'taxonomy' => 'tr-betriebsteil',
'field' => 'name',
'terms' => array('Alle', $betriebsteil),
)
);

}
return $query_args;
}

#2745020

Christopher Amirian
Supporter

Languages: English (English )

Hi,

It is very hard to check your code as this is part of custom development and outside of our support scope. I will do my best with the information you have provided but I can not guarantee.

The issue you're facing likely stems from the fact that your custom tax_query overrides the existing query, which includes the filters for ld_course_category and ld_course_tag. When you set tax_query directly, it replaces any previous taxonomy queries, including the ones related to those filters.

To fix this, you need to merge your custom taxonomy query with any existing tax_query conditions, so that the original filters for ld_course_category and ld_course_tag are preserved.

Here's an updated version of your function that checks if a tax_query already exists and merges your custom conditions with the existing ones:

add_filter( 'wpv_filter_query', 'filter_toolkit_course_enrolled_user_toolkit', 102, 3 );
function filter_toolkit_course_enrolled_user_toolkit( $query_args, $view_settings, $view_id ) {

    $view_ids = array(7979,8118);

    if (isset($view_settings['view_id']) && in_array($view_settings['view_id'], $view_ids )) {

        $user_id = get_current_user_id();
        $kostenstelle = get_user_meta($user_id , 'wpcf-std-kostenstelle', true);
        $qualifikation = get_user_meta($user_id , 'wpcf-std-qualifikation', true);
        $bereich = get_user_meta($user_id , 'wpcf-std-bereich', true);
        $funktion = get_user_meta($user_id , 'wpcf-std-funktion', true);
        $betriebsteil = get_user_meta($user_id , 'wpcf-std-betriebsteil', true);

        // Prepare your custom tax_query
        $custom_tax_query = array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'tr-kostenstelle',
                'field'    => 'slug',
                'terms'    => array('Alle', $kostenstelle),
            ),
            array(
                'taxonomy' => 'tr-qualifikation',
                'field'    => 'name',
                'terms'    => array('Alle', $qualifikation),
            ),
            array(
                'taxonomy' => 'tr-bereich',
                'field'    => 'name',
                'terms'    => array('Alle', $bereich),
            ),
            array(
                'taxonomy' => 'tr-funktion',
                'field'    => 'name',
                'terms'    => array('Alle', $funktion),
            ),
            array(
                'taxonomy' => 'tr-betriebsteil',
                'field'    => 'name',
                'terms'    => array('Alle', $betriebsteil),
            )
        );

        // Check if there's an existing tax_query and merge them
        if (isset($query_args['tax_query'])) {
            $query_args['tax_query'] = array_merge($query_args['tax_query'], $custom_tax_query);
        } else {
            $query_args['tax_query'] = $custom_tax_query;
        }
    }

    return $query_args;
}

This version checks if tax_query already exists in $query_args and appends your custom query to it. This way, the original filters for ld_course_category and ld_course_tag are preserved while also applying your custom logic.

This is what I could detect. Other than that I have no idea.

Thank you.

#2745441

Dear Christopher, that is exactly what we have forgotten. Thank you so much for your awesome support. I can't say it often enough: With Toolset we take our websites to a whole new level. With almost no programming knowledge required. 🙂 Have a nice day!