- This topic has 4 replies, 3 voices, and was last updated 5 years, 1 month ago by
Juan.
-
AuthorPosts
-
-
November 22, 2016 at 5:23 am #7582
daprelaParticipantI need to create a select option which has not a fixed number of options but they are calculated every time based on other conditions. Is that possible?
-
November 22, 2016 at 7:39 pm #7585
JasonKeymasterHi @deprela!
Sure! It’s just normal PHP so you can set choices to be whatever you need. You can use get_posts, wp_get_post_terms, or any other function to get the values and then set them as the choices (where the key is the value stored in the database and the value is the label presented to the user). You can even use the piklist function to pluck the values from collections:
piklist('field', array( 'type' => 'select', 'field' => 'related_post', 'label' => 'Related Post', 'choices' => piklist(get_posts(array( 'post_type' => 'post', 'numberposts' => -1 )), array('ID', 'post_title')) ));Hope this helps!
-
December 15, 2016 at 9:33 pm #7662
JuanParticipantHi @Jason!
is it posible to add a default first choice? something like: “Select one post…”
thanks!
piklist('field', array( 'type' => 'select', 'field' => 'staff_member', 'label' => 'Add Staff Members', 'columns' => 12, 'add_more' => true, 'choices' => piklist(get_posts(array( 'none' => 'Select one post…' , 'post_type' => 'staff', 'numberposts' => -1 )), array('ID', 'post_title')) )); -
December 16, 2016 at 12:04 pm #7664
JasonKeymasterHi @Juan!
It sure is! Just like this:
piklist('field', array( 'type' => 'select', 'field' => 'staff_member', 'label' => 'Add Staff Members', 'columns' => 12, 'value' => '', 'add_more' => true, 'choices' => array('' => 'Select one post...') + piklist(get_posts(array( 'post_type' => 'staff', 'numberposts' => -1 )), array('ID', 'post_title')) ));Notice two things here: First, I added a
'value' => ''line that tells Piklist what the default value of the field is when there’s no value saved, yet. Second, I added thearray('' => 'Select one post...') +line. This adds an option to the beginning of the choices with a stored value of nothing, while displaying ‘Select one post…’Hope this helps! 🙂
-
December 16, 2016 at 12:06 pm #7665
JuanParticipantthanks Jason!
-
-
AuthorPosts
- The topic ‘Creating dynamic select’ is closed to new replies.