• Resolved lazyym

    (@lazyym)


    Is there a way to restrict users to posting in only one category? I notice in the post form it allows users to select multiple categories when posting.

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

    (@gwin)

    You can use the code snippet here https://github.com/simpliko/wpadverts-snippets/blob/master/limit-category-selection/limit-category-selection.php it allows users to select only one category.

    Thread Starter lazyym

    (@lazyym)

    Perfect! Thank you!

    Is it possible to limit category selections to a specific number ? By using the snippet i did $form["field"][$i]["max_choices"] = 3; but that did not help

    Plugin Author Greg Winiarski

    (@gwin)

    It should be possible, can you paste your whole code snippet here?

    I can see the changed made to max choices, but in front end it doesn’t have affect.

    add_filter("adverts_form_load", "limit_category_selection");
    function limit_category_selection( $form ) {
        if($form["name"] != 'advert' || is_admin()) {
            return $form;
        }
        $count = count( $form["field"] );    
        for( $i = 0; $i < $count; $i++ ) {
            if($form["field"][$i]["name"] == "advert_category") {
                $form["field"][$i]["max_choices"] = 3;
            }
        }
        print_r($form["field"]);
        return $form;
    }

    Category selection
    url to my website

    Plugin Author Greg Winiarski

    (@gwin)

    Your code seems correct, but it looks like there is some bug in WPAdverts which does not validate the max selected categories.

    I will have this fixed in next WPAdverts release.

    Sounds good. I will keep the code in place. Thanks

    This still not fixed in update

    Plugin Author Greg Winiarski

    (@gwin)

    It is fixed but you will need a different code

    
    add_filter("adverts_form_load", "limit_category_selection");
    function limit_category_selection( $form ) {
        if($form["name"] != 'advert' || is_admin()) {
            return $form;
        }
        $count = count( $form["field"] );
        for( $i = 0; $i < $count; $i++ ) {
            if($form["field"][$i]["name"] == "advert_category") {
                $form["field"][$i]["max_choices"] = 3;
                $form["field"][$i]["validator"] = array(
                    array(
                        "name" => "max_choices",
                        "params" => array( "max_choices" => 3 )
                    )
                );
            }
        }
        return $form;
    }
    

    that did not work either.
    screen shot
    website

    Plugin Author Greg Winiarski

    (@gwin)

    I am testing it on your site and it seems to be working fine for me, note that like any other validator in WPAdverts it is executed after the form is submitted, so while you can select all the categories when you click submit button you will be redirected back to the form which will show a validation error.

    Oh sorry, i was assuming it will work live on the go, like disabling others after 3 options checked or something like that. But thank.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Restrict to post in single category?’ is closed to new replies.