Members can only add listings in only choosen category

Hello dear Hivepress Community,
first of all, thank you for this forum and helping each other.

Question: Can I generally forbid all users to make entries in all areas, except for the area I allow?

Example: Users cannot create hotel listings, but users can add their products to the Category “Hardware Marketplace” section. But can they continue to rate the hotel entries (award stars)?

(I dont have the Marketplace plugin - i am using ListingHive and hivepress)

Thank you in advance

Hi,

Unfortunately, there’s no such feature, it would require a custom implementation. If customizations are required for your site, please try customizing it using the collection of code snippets Search · user:hivepress · GitHub and other developer resources, or consider hiring someone for custom work Customize your website | HivePress

1 Like

Thank you andrii, sorry for my late answer but finally i fixed it and found an possible way. :heart:

First of all, i deleted with an css snippet the “category” field at “add listing” page with following code:

.hp-form__field--select:has(select[name="categories"]) {
    display: none !important;
}

than i addet an custom functions.php snippet to force/allow only add normal members in the choosen category (in my case “hardware marktplatz”) with the following code:

<?php

add_action( 'hivepress/v1/models/listing/create', 'auto_assign_hardware_marketplace_category_frontend', 10, 1 );

function auto_assign_hardware_marketplace_category_frontend( $listing ) {
    if ( ! is_admin() && ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { // Nur für Frontend-Erstellungen durch Nicht-Admins
        $hardware_category_id = 38; // Ersetze dies mit der tatsächlichen ID deiner "Hardware Marktplatz" Kategorie

        wp_set_post_terms( $listing->get_id(), $hardware_category_id, 'hp_listing_category' );
    }
}

Result: Members can only add listings in only choosen category (by my self for all users), so they click to “add listing” and fill the “selling form” and list it.

For other categories now i can only use the backend admin page.

Thank you again, and thanks for gemini helping me with the code snippet’s :slight_smile: :heart:

1 Like

Hi,

Thank you for sharing this solution, it will be useful for our community.

1 Like

I would like to give an update for other users in case it might be helpful.

With my first solution there were problems, namely because the “category” could not be selected because it was hidden via CSS, the correct forms were not loaded, only a standard form without important properties.

Later there was also the problem that newly registered users first had to fill in their profile so that they could also publish new listings, but the CSS somehow intervened and you couldn’t update your profile because the button was no longer clickable.

Now the improved solution in a .php snippet step:
(Change the Category ID for your own, in my case it’s ID:38)

add_filter( 'hivepress/v1/forms/listing_submit', function( $form ) {
    if ( ! current_user_can( 'manage_options' ) ) {
        unset( $form['fields']['categories'] );
    }
    return $form;
}, 1000 );

add_action( 'hivepress/v1/models/listing/create', function( $listing_id ) {
    if ( ! current_user_can( 'manage_options' ) ) {
        wp_set_object_terms( $listing_id, 38, 'hp_listing_category' );
    }
}, 10 );

Hope it helps you, best thanks, also to the Hivepress team :heart:

1 Like

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