• Resolved cavedeb

    (@cavedeb)


    I’m looking for a way to filter the “User Registered Data Source” in a form’s custom fields. When using the user registered data source, the only option is “Adverts Categories.”

    We will offer two types of listings on our site:
    -Items for sale or trade, and members will be able to list as many of those as they like for free.
    -Directory listings to advertise a business in general, available for a fee to members and non members alike.

    These two types of listings require separate forms, the first available only to paid members, with only the item categories. The second form is available to all users, and the available categories should only be business type.

    How can we restrict the categories, but keep them associated with the adverts categories so the postings automatically show up in the category searches?

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    if i understand correctly you would like to have two different [adverts_add] forms and the forms would have different categories.

    There are two ways to do that:

    1. the easiest (that is not requiring any programming knowledge, but might require quite a lot of manual work if you have a lot of categories), would be to edit a category field in each form.

    In “Fill Method” select “I will enter options myself” and check the “use values” checkbox.

    Finally, manually fill the available categories for this form, the options values need to be equal to category IDs the option labels can be the same or similar as the category titles.

    When you save the form, it should have only the predefined list of options / categories when posting an Ad in the frontend.

    2. the second method would require creating two data sources as explained here https://wpadverts.com/documentation/custom-fields-paid-addon/, each data source function would need to somehow filter which categories are assigned to this data source.

    
    add_action( "init", function() {
        wpadverts_custom_fields_register_data_source(array(
            "name" => "adverts-categories",
            "title" => __( "Adverts Categories (Trade)", "wpadverts-custom-fields" ),
            "callback" => "adverts_taxonomies_trade"
        ));
        wpadverts_custom_fields_register_data_source(array(
            "name" => "adverts-categories",
            "title" => __( "Adverts Categories (Business)", "wpadverts-custom-fields" ),
            "callback" => "adverts_taxonomies_business"
        ));
    } );
    function adverts_taxonomies_trade( $taxonomy = 'advert_category' ) {
        $args = array(
            'taxonomy'     => $taxonomy,
            'hierarchical' => true,
            'orderby'       => 'name',
            'order'         => 'ASC',
            'hide_empty'   => false,
            'depth'         => 0,
            'selected' => 0,
            'show_count' => 0,
            
        );
    
        include_once ADVERTS_PATH . '/includes/class-walker-category-options.php';
        $walker = new Adverts_Walker_Category_Options;
        $params = array(
            get_terms( $taxonomy, $args ),
            0,
            $args
        );
        return call_user_func_array(array( &$walker, 'walk' ), $params );
    }
    function adverts_taxonomies_business( $taxonomy = 'advert_category' ) {
        $args = array(
            'taxonomy'     => $taxonomy,
            'hierarchical' => true,
            'orderby'       => 'name',
            'order'         => 'ASC',
            'hide_empty'   => false,
            'depth'         => 0,
            'selected' => 0,
            'show_count' => 0,
            
        );
    
        include_once ADVERTS_PATH . '/includes/class-walker-category-options.php';
        $walker = new Adverts_Walker_Category_Options;
        $params = array(
            get_terms( $taxonomy, $args ),
            0,
            $args
        );
        return call_user_func_array(array( &$walker, 'walk' ), $params );
    }
    

    The code you can paste in your theme functions.php, note that by default both taxonomies list all of the Adverts Categories you would need to somehow customize the $args variable in adverts_taxonomies_business() and adverts_taxonomies_trad() functions so they return only categories which should be available in a trade or business form.

    One way to do that would be to add a custom field in wp-admin / Classifieds / Categories panel when editing a category and mark each category “trade” or “business”.

    How to add a field to custom taxonomy you would need to read in some online tutorial, for example https://www.ibenic.com/custom-fields-wordpress-taxonomies/

    Thread Starter cavedeb

    (@cavedeb)

    Thanks so much. I had missed the “use values” checkbox at first, so using it I was able to set up my forms without the user registered data source. However, when specifying the form to display on public pages in the shortcode, it still displays the first form I created, even after unchecking the default scheme in that form. I believe I followed the instructions exactly from your documentation:

    Applying Form Schemes To Shortcodes
    When creating or editing form you can select that the form scheme is default scheme and it will be automatically applied when the form is rendered. For forms in [adverts_add] and [adverts_list] it is possible to override the default setting with “form_scheme” param.

    For example, if you have a form named “cars”, you can apply it to the [adverts_add] shortcode by changing the shortcode to

    [adverts_add form_scheme=”cars”]

    The form names you can find in wp-admin / Classifieds / Options / Custom Fields panel.

    In this case I replaced “cars” with “directory”. What am I missing?

    Plugin Author Greg Winiarski

    (@gwin)

    You seem to have it setup correctly, the most common problem is that the quote (“) character used in the shortcode is converted to some UTF-8 character by WordPress.

    If you are using Gutenberg please use the Shortcode block and type the shortcode from the keyboard, if you are using classic editor then click “Text” tab in the editor and type the shortcode from the keyboard there.

    If this will not work it would be best if you could paste here screenshots from your Custom Fields configuration and the page with shortcode.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Restricting category options in custom fields’ is closed to new replies.