Limit suggest field to 1 or X number of entries...

I’m working on a custom module that will display a custom post type for a client. I want to provide the client with 2 fields…the first should allow them to choose only 1 post while the second field should allow them to choose 5 posts. They can choose less than 5 but not more than that.

Looking further at the suggest field I see that it’s a simple text field and it looks like the data is stored as a comma separated list. Is there a built in way to limit the number of items that can be selected?

Hey Matt,

I’ve already assigned another member of the team who can assist you with your concern.

Ben

Thanks Ben!

I also just noticed something else this morning…the suggest field doesn’t honor the order of entry. So for example, if I enter 5 posts by title into the field and save the module, when I return to edit the module it reorders the posts in the field for some reason.

Hey Matt,

There isn’t a built in way to limit the selection, although I looked at the docs for the auto suggest library and that is something that we can add easily. I don’t have an ETA on that, but I have added it to the list. We’ll also have to look into the order issue and see if there is something we can do there. I’ll post back when we have an update on that.

Justin

Ah, thanks Justin. So probably using data attributes and appending the option to the _initAutoSuggestField method in fl-builder.js where the option is taken from the new data attribute.

I look forward to your solution on the order issue.

Yep! You can hack that together now if you want so you have it. I’ll get selectionLimit in the next release. As for order, you can change line 149 of classes/class-fl-builder-auto-suggest.php to this to get that working…

$posts = $wpdb->get_results("SELECT ID, post_title FROM {$wpdb->posts} WHERE ID IN ({$ids}) ORDER BY FIELD(ID, {$ids})");

Justin

Thanks Justin!