Add ajax search feature

Hello dear community!

I’m building a rental marketplace using the RentalHive theme, and I need your assistance with improving the user experience on the “Explore Vehicles” (ex Properties) page where the search filters appear.

Currently, the filter system loads with “All Categories” selected by default, which doesn’t show relevant attributes. In order for users to see attributes specific to each category (like Motorbikes, Cars, or Boats), they have to manually:
1. Select a category (radio button), and then
2. Click the “Filter” button for the form to apply.

This process is unintuitive and causes friction.
I’d like to make it so that as soon as a user selects a category, the filter form is submitted automatically — just as if they clicked the “Filter” button manually.

Here’s what I’ve already tried with no success:
• Injected JavaScript to call .submit() on the .hp-listings-filter form when a category is changed.
• Tried triggering hpFilter.submit() (but it’s undefined globally).
• Simulated a .click() on the “Filter” button — no result.
• Tried full MouseEvent(‘click’, {…}) dispatching on the button, which also didn’t trigger the listing refresh.
• Attempted injecting the selected category into the form and then submitting it manually.
• Wrapped all the above inside DOMContentLoaded and used both vanilla JS and jQuery — no impact.

Despite all attempts, the listings only update when the “Filter” button is manually clicked by the user.

Could you kindly:
• Suggest the correct approach or code snippet for making the form auto-submit when a category radio button is changed?
• Or confirm if it’s currently possible to achieve this using your internal APIs or JavaScript events?

This UX improvement is critical for us, and I’d truly appreciate your help or a working code example.

Thanks in advance!

Hey,

I’m not HivePress staff, or on my computer at the moment, but you might be able to achieve what you’re looking for by simply adjusting the settings in your attributes to allow for them to be filterable. I forget what all of the options are, but I think this is do-able without customization. Forgive me, if I’ve got this wrong! :slight_smile:

Hey Chris! Thanks for the suggesting, but I’m afraid it’s not the exact solution that will work for us :slight_smile: Filterable attributes does not affect the way Categories are getting displayed on the Filter page, I’ve tried pretty much everything, so the only option left is custom coding :sweat_smile:

1 Like

Hey @northbrightt,

Sorry that wasn’t much help! I’ve had a play around with the settings and can re-create the issue myself. I had a go at solving this for you and I think I’ve come up with a working snippet:

function enqueue_auto_submit_script() {
    wp_add_inline_script(
        'hivepress-core-frontend',
        "
        document.addEventListener('DOMContentLoaded', function() {
          const categoryRadios = document.querySelectorAll('input[name=\"_category\"]');
          if (categoryRadios.length === 0) {
            console.error('No category radio buttons found. Verify the name attribute is \"_category\".');
            return;
          }
          console.log('Found', categoryRadios.length, 'category radio buttons');
          categoryRadios.forEach(radio => {
            radio.addEventListener('change', function() {
              console.log('Category selected:', this.value);
              const form = this.closest('form');
              if (form) {
                console.log('Form found, submitting now');
                const submitButton = form.querySelector('button[type=\"submit\"]');
                if (submitButton) {
                  submitButton.click();
                } else {
                  form.submit();
                }
              } else {
                console.error('No form found containing the radio button');
              }
            });
          });
        });
        "
    );
}
add_action('wp_enqueue_scripts', 'enqueue_auto_submit_script');

This works as you described in your OP by submitting the form upon selecting a category/radio button, and thus not requiring the ‘Filter’ button to be clicked.

I hope this helps!

Cheers,
Chris :victory_hand:

1 Like

Hey Chris,

I was literally screaming when tried your snippet and it worked!:grinning_face_with_smiling_eyes:
Thanks so much for your help! It worked, and worked in the exact way I wanted, you’re LEGEND!

Appreciate your help big time🙏

And I hope this will help other users someday if they encounter the same issue :slight_smile:

Have a great day!

Best regards,
Norbert

1 Like

Hey,

Haha, that’s great to hear! You’re very welcome and I’m happy to have been able to help!

Best of luck with your site! :smiling_face:

Cheers,
Chris :victory_hand:

1 Like

so happy this worked for you. can i see it working somewhere ?
and where was this code inserted ? was it as a Php snippet ?

Hey @unitehearts.in,

I added the snippet via the Code Snippets plugin. I don’t have a test site that I’m able to show you, but this snippet makes it so that if you’re on the search page with the categories listed at the side as radio buttons. If you click a category name, it will refresh the page to filter the results without you having to manually click the ‘Filter’ button after choosing a category.

Once the page refreshes, the category-specific attributes can be displayed in the side bar to further filter your search.

I hope that makes sense!

Cheers,
Chris :victory_hand:

1 Like

Hello mate,

Sorry, also don’t have a test site to show how it works, mine is under maintenance. As soon as will finish everything, will provide a link for you to check🙌🏻
For now, you can try adding this snippet via Code Snippets plugin as Chris mentioned, it’s not breaking the structure of a website or anything, so it’s quite safe to apply and check :slight_smile:

Regards,
Norbert

1 Like

is it a php snippet or javascript ?
i tried in php and nothing happened

Hey mate,

It’s a PHP snippet, specifically targeting Categories selection radio buttons of the “Properties” page. Make sure your snippet is set to run everywhere and it’s active.
I can confirm that it works well!

Best regards,
Norbert

nothing happened…
do i have to something to categories too ?

Hi,

Please try this code snippet:

add_action(
	'wp_enqueue_scripts',
	function () {
		wp_add_inline_script(
			'hivepress-core-frontend',
			"
			jQuery(document).ready(function($) {
				$('.hp-form--listing-filter input[name =\"_category\"]').on('change', function () {
					$(this).closest('form').submit();
				});
			});
			"
		);
	},
	1000
);

It re-submits the form when the category is selected in the search filters form.

Hope this helps

2 Likes

thanks @ihor
do i have to use just your snippet or yours and the one above both ?
i tried with just yours and nothing changes.
is there something extra needed like some jquery code enabling needed etc.
My categories are showing as a Drop Down select box - only one selection allowed now.
When I select one, nothing changes. I have to press Search after that.

Hey @unitehearts.in

Just add one of the provided code snippets, there’s no point of adding both.
Please check instructions on how to add a snippet from HivePress:

And just to be clear, those snippets affect Categories selection on the Properties page specifically.

Regards,
Norbert

thanks. it worked now, when i moved Categories from Search to Filter zone.

1 Like