Skip Navigation

[Resolved] Code snippet not running on Ajax calls on a search form

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 2 replies, has 2 voices.

Last updated by Minesh 1 year, 5 months ago.

Assisted by: Minesh.

Author
Posts
#2627141

I have a code snippet installed on the Custom Code section of Toolset Settings that excludes child categories from a search filter. You can see the search here: hidden link

I have it set to run always and everywhere, including on Ajax calls. However, when I use ajax pagination or ajax to clear search filters, the code doesn't run. However, when I use ajax to search, the code still works. The code follows:

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

add_shortcode('limit_categories', 'limit_categories_func');
function limit_categories_func($atts){
extract( shortcode_atts( array(
'categories' => '',
), $atts ) );
$categories_arr = explode(',', $categories);
return implode(',', array_slice($categories_arr, 0, 1));
}

function func_display_only_parent_terms( $query ){
global $post;

if ( $post->ID == 944 && $query->query_vars['taxonomy'][0] == 'category' && !is_admin() ) {

$query->query_vars['parent'] = 0;

}
if
( $post->ID == 18 && $query->query_vars['taxonomy'][0] == 'category' && !is_admin() ) {

$query->query_vars['parent'] = 0;

}
}
add_action( 'pre_get_terms', 'func_display_only_parent_terms',10,1);

#2627279

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

It seems to access the problem URL you shared, we will require access details.

Can you please send me admin access details as well as exact steps I will have to follow to see the issue?

*** 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.

#2627639

Minesh
Supporter

Languages: English (English )

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

I've disabled the "pre_get_terms" filter you are using that is added to "Custom Code" section offered by Toolset. Instead of "pre_get_terms" hook, I've added the following Toolset view hook: wpv_filter_taxonomy_frontend_search_get_terms_args

add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'func_show_only_parent_terms', 10, 3 );
function func_show_only_parent_terms( $args, $taxonomy, $view_id ) {

  $allowed_view_ids = array(946,565);

  if (in_array($view_id, $allowed_view_ids )) {
      
    	$args['parent'] = 0;
   }
    return $args;
}

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_frontend_search_get_terms_args

Can you please check now and confirm it works as expected on the following URLs:
- hidden link
- hidden link

#2627799

This fixed my issue - thanks so much for the great support.