Skip Navigation

[Resolved] Filtering search query

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/Karachi (GMT+05:00)

This topic contains 9 replies, has 2 voices.

Last updated by Waqar 11 months ago.

Assisted by: Waqar.

Author
Posts
#2680479
ExcursionsView_SearchAndPagination.png
PropertiesView_SearchAndPagination.png
ExcursionsView_CustomSearchSettings.png
PropertiesView_CustomSearchSettings.png
PHPSnippet_LimitExcursionSearchResultsToCurrentTerm.png
PHPSnippet_LimitPropertySearchResultsToCurrentTerm.png
frontend.png

Tell us what you are trying to do?
We have a custom hierarchical taxonomy for destinations. I have created an Elementor template for destination archives.
We have these custom post types which use the destinations taxonomy.
* Properties
* Excursions
On each destination archive "page", we have a section each for Properties and Excursions.
* For the Properties section, we want to only show Properties (not Excursions) for the current Destination (including child destinations), as well as have name search, plus filters for rating, destination (self and child destinations only) and collection (another taxonomy that is flat).
* For the Excursions section, we want to only show Excursions (not Properties) for the current Destination (including child destinations), as well as have name search, plus a filter for destination (self and child destinations only).

Please see the screenshot for clarity or go to hidden link

We are limited to WordPress.com at this time, thus I am using the Snippets plugin to add PHP functions. I am currently trying to limit the search results to the current destination + children.

Limit Property Search Results to Current Term:
add_filter( 'wpv_filter_query', 'filter_include_cat_fn', 1000 , 3 );
function filter_include_cat_fn( $query_args, $view_settings ) {
// check if specific view
if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 14020) ) {
// set the parent tax term filter if no tax filter is set
if(empty($query_args['tax_query']))
{
$query_args['tax_query'][0]['taxonomy'] = 'destinations';
$query_args['tax_query'][0]['field'] = 'id';
$query_args['tax_query'][0]['terms'][0] = get_queried_object_id();
$query_args['tax_query'][0]['operator'] = 'IN';
$query_args['tax_query'][0]['include_children'] = true;
$query_args['tax_query']['relation'] = 'AND';
}
}

return $query_args;
}

This works however the search/filters/reset are broken.

Limit Excursion Search Results to Current Term:
add_filter( 'wpv_filter_query', 'filter_include_cat_fn2', 1001 , 3 );
function filter_include_cat_fn2( $query_args, $view_settings ) {
// check if specific view
if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 14035) ) {
// set the parent tax term filter if no tax filter is set
if(empty($query_args['tax_query']))
{
$query_args['tax_query'][0]['taxonomy'] = 'destinations';
$query_args['tax_query'][0]['field'] = 'id';
$query_args['tax_query'][0]['terms'][0] = get_queried_object_id();
$query_args['tax_query'][0]['operator'] = 'IN';
$query_args['tax_query'][0]['include_children'] = true;
$query_args['tax_query']['relation'] = 'AND';
}
}

return $query_args;
}

Similar to above, the search/filters/reset are broken, except the destination filter.

Is there any documentation that you are following?
https://toolset.com/forums/topic/create-a-view-to-limit-results-to-a-parent-taxonomy-then-filter-by-child/

Is there a similar example that we can see?
Sorry, I do not have a functioning example yet.

What is the link to your site?
hidden link

#2680550

Hi,

To troubleshoot this and suggest the next steps, I'll need to see exactly how this view is set up in the admin area.

Can you please share temporary admin login details, along with the link to the page with this view?

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

regards,
Waqar

#2680665

Hi, I'm not sure if you can see, but the info is in the private message. Thank you.

#2680969

Thank you for sharing the admin access.

The JetPack plugin's "Secure Sign On" feature is not allowing me to access the website's admin area.
( ref: hidden link )

Can you please disable this feature from the JetPack's settings, during this investigation?

#2681097

Hi Waqar, I've done as you requested.

#2681977

Thank you for waiting, as I was away for a day off.

I can now access the website's admin login page, but it is showing the incorrect password message.

Can you please test the username and password again? I'm setting your next reply as private.

#2682833

Thank you for waiting while I performed some tests on my website.

Even with the same settings and custom code, I couldn't reproduce this behavior on my test website. This suggests that something specific to your website is involved.

Do I have your permission to download a clone/snapshot of the website? This will help in investigating this on a different server.

#2683031

Please proceed.

I noticed Toolset also has an area for PHP snippets - please let me know if that is a more appropriate place for custom code than the Snippets plugin. Thank you.

#2684075

Just wanted to let you know that I'm still working on this and will share the findings with you today.

#2684387

Thank you for waiting, as this troubleshooting turned out to be more complicated.

During testing, I discovered that the cases in which the filtering and resetting of views fails is when 'get_queried_object_id()' function doesn't return the current taxonomy terms ID.
( as the view is being used in the taxonomy archive)

To overcome this, I added some changes to the code snippet, so that the same taxonomy term ID can be acquired through an alternate approach:


add_filter( 'wpv_filter_query', 'filter_include_cat_fn', 1000 , 3 );
function filter_include_cat_fn( $query_args, $view_settings ) {
    // check if specific view
    if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 14020) ) {

        $current_archive_term_id = get_queried_object_id();
        if(empty($current_archive_term_id)) {
            $current_archive_term_id = $_POST['environment']['archive']['data']['term_id'];
        }

        // set the parent tax term filter if no tax filter is set
        if(  empty($query_args['tax_query'])  )
        {
            $query_args['tax_query'][0]['taxonomy'] = 'destinations';
            $query_args['tax_query'][0]['field'] = 'id';
            $query_args['tax_query'][0]['terms'][0] = $current_archive_term_id;
            $query_args['tax_query'][0]['operator'] = 'IN';
            $query_args['tax_query'][0]['include_children'] = true;
            $query_args['tax_query']['relation'] = 'AND';
        }
    }

    return $query_args;
}

You can implement these changes in the code snippet for both views on this archive page.