Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Dummy/Empty Item generated in results › Reply To: Dummy/Empty Item generated in results
While adding this code, should I enable the user search option?
Also this code will not conflict with other code of asp? I have below codes added currently
// Hook into Ajax Search Pro’s form rendering to inject a hidden input with the category ID
add_action(‘asp_layout_in_form’, ‘asp_layout_in_form_archive_input’, 10, 1);
function asp_layout_in_form_archive_input($search_id) {
// Only apply to these instance IDs
$allowed_ids = array(11, 12); // update these as needed
if (!in_array($search_id, $allowed_ids)) return;
global $wp, $post;
$term = null;
// 1. On a category archive page (handles nested child categories too)
if (!empty($wp->query_vars[‘category_name’])) {
$category_path = $wp->query_vars[‘category_name’];
$slugs = explode(‘/’, $category_path); // For nested categories
$last_slug = end($slugs); // Use the final slug
$term = get_term_by(‘slug’, $last_slug, ‘category’);
}
// 2. On search results page, get category from URL parameter
if (isset($_GET[‘asp_tax_archive’])) {
$term_id = intval($_GET[‘asp_tax_archive’]);
$term = get_term($term_id, ‘category’);
}
// Output hidden input field if valid term is found
if (!empty($term) && !is_wp_error($term)) {
echo ‘<input type=”hidden” name=”asp_tax_archive” value=”‘ . esc_attr($term->term_id) . ‘” data-asp-hidden-field>’;
}
}
// Modify the Ajax Search Pro query arguments to restrict search results to a specific taxonomy term (e.g., category)
add_filter(‘asp_query_args’, ‘asp_archive_page_category_restriction’, 10, 2);
function asp_archive_page_category_restriction($args,$search_id) {
// Only apply to these instance IDs
$allowed_ids = array(11, 12);
if (!in_array($search_id, $allowed_ids)) return $args;
$archive_term_id = null;
// Case 1: If the search options are submitted via POST, extract the hidden category ID
if (isset($_POST[‘options’])) {
parse_str($_POST[‘options’], $so);
if (!empty($so[‘asp_tax_archive’])) {
$archive_term_id = $so[‘asp_tax_archive’];
}
}
// Case 2: If the search was triggered via a GET request, fetch the category ID from the URL
elseif (!empty($_GET[‘asp_tax_archive’])) {
$archive_term_id = $_GET[‘asp_tax_archive’];
}
// If a valid taxonomy term ID is present, modify the search query to filter by that term
if ($archive_term_id) {
$term = get_term($archive_term_id);
if (!is_wp_error($term)) {
$args[‘post_tax_filter’][] = array(
‘taxonomy’ => $term->taxonomy, // e.g., ‘category’
‘include’ => array($term->term_id), // Term ID to include
‘exclude’ => array(), // No exclusions
‘logic’ => ‘AND’, // Combine filters using AND logic
‘allow_empty’ => false // Do not allow empty result sets
);
}
}
return $args;
}