Preselected category (with current category value) when i add a listing

hi, i repeat my previous question once again because before 2 days there was a new version (v1.6.4) with new characteristics about category selection.

i want to know if is possible….
a user explore a specific listings, for example category “Cars” listings
a user click the “+Add listing” button on a left upper corner
on a new add listing page the category is preselected with the current category (“Cars”)

is it possible?

I don’t want for a user to select a category but the category must be preselected based on a current category.

Hi,
There’s no such feature available by default, but it’s possible with customizations, if you’re familiar with customizations I hope this guidance helps:

  1. You can override the Add Listing button template part via a child theme, adding category ID to the button URL.
  2. Then you can add a custom function that filters the listing form and pre-selects the category based on the URL parameter.
  3. This may require extra debugging because the listing submission process has multiple steps.
1 Like

thanks but i can’t do the customizations myself.
Maybe you can describe it analytically or add this feature (as an option) to a newer version.

Sorry, there’s no simple code snippet for this, but we’ll consider adding this option.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

Hello! I’m trying to generate URLs that take users to the Submit Listing page with a specific category already selected. I’ve spent hours on this but haven’t had any luck—the closest I’ve gotten is preselecting the category, yet the form still won’t render.

Hi,

Please check this topic. Also, if you need help with customization, you can always submit a request or hire an expert: Customize your website | HivePress

I hope it helps

Can you guide me on how to achieve this as i am not interested in hiring due to high cost. I have already asked. I am running a small business so i hope you understand

Hello there,

Based on this solution, you may come up with your own.

2 Likes

Hi Condorito, thanks alot that helps! i have managed to do it thank you! In case anyone is wondering how to please see the code im about to share

function add_inline_jquery_script() {
    $current_uri = $_SERVER['REQUEST_URI'];
    $base_path = '/submit-listing/details/';
    
    if (strpos($current_uri, $base_path) === false) return;

    $preset_listing_cat_id = 0;
    $allowed_categories = [35, 36, 232, 262];

    // URL parameter check
    if (isset($_GET['preset_category'])) {
        $url_category = intval($_GET['preset_category']);
        if (in_array($url_category, $allowed_categories)) {
            $preset_listing_cat_id = $url_category;
        }
    } else {
        // Vendor-based logic
        $vendor = \HivePress\Models\Vendor::query()->filter([
            'status__in' => ['auto-draft', 'draft', 'publish'],
            'user'       => get_current_user_id(),
        ])->get_first();

        if ($vendor && method_exists($vendor, 'get_categories__id')) {
            $vendor_cat_ids = $vendor->get_categories__id();
            $current_vendor_cat_id = !empty($vendor_cat_ids) ? intval(reset($vendor_cat_ids)) : 0;

            switch($current_vendor_cat_id) {
                case 172: $preset_listing_cat_id = 58; break;
                case 174: $preset_listing_cat_id = 76; break;
            }
        }
    }

    if ($preset_listing_cat_id !== 0) {
        ?>
        <script type="text/javascript">
        jQuery(function($) {
            // Wait for form elements to load
            function initializeForm() {
                var $select = $("select[name='categories']");
                
                if ($select.length) {
                    // Set category and force form refresh
                    $select.val(<?php echo $preset_listing_cat_id; ?>)
                            .trigger('change')
                            .trigger('select2:select'); // If using Select2
                    
                    // Additional form initialization
                    if (typeof hivepress !== 'undefined') {
                        hivepress.observer.trigger('init');
                    }
                    
                    // Optional: Hide dropdown
                    <?php if (isset($_GET['hide_dropdown'])): ?>
                    $select.parent().hide();
                    <?php endif; ?>
                } else {
                    setTimeout(initializeForm, 500);
                }
            }
            
            // Start initialization
            setTimeout(initializeForm, 100);
        });
        </script>
        <?php
    }
}
add_action('wp_footer', 'add_inline_jquery_script');
2 Likes

Perhaps if you would allow pre-population of form fields by request through _GET, one would only have to construct a URL. and it would open up endless possibilities.

Hi,

Thank you for your feedback. We’ve added this feature to the backlog and will consider adding it in future updates.