Title: [Plugin: Custom Field Template] Dynamic select list
Last modified: August 19, 2016

---

# [Plugin: Custom Field Template] Dynamic select list

 *  [Derek Ashauer](https://wordpress.org/support/users/sccr410/)
 * (@sccr410)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/)
 * Is there a way using Custom Field Template to populate a select list dynamically?

Viewing 15 replies - 1 through 15 (of 21 total)

1 [2](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/page/2/?output_format=md)

 *  [daveleeone](https://wordpress.org/support/users/daveleeone/)
 * (@daveleeone)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/#post-1963532)
 * I am also interested.
 * my current hack with jquery css is:
 * ======
    Custom Field template: ——
 *     ```
       [Books]
       type = select
       value = categoryA # categoryB
       label = Books
       search = true
       output = true
   
       [Books_categoryA]
       size = 35
       type = text
       label = CategoryA
       search = true
       output = true
   
       [Books_categoryB]
       size = 35
       type = text
       label = CategoryB
       search = true
       output = true
       ```
   
 * =======
    jquery for the backend: ——-
 *     ```
       $("#books1_0").change(function () {
                 var str = "";
                 $("#books1_0 option:selected").each(function () {
                       str += $(this).text() + " ";
                     });		
   
               $("#dl_category12_0").removeClass("select");
       		$("#dl_books_categoryA2_0").removeClass("select");
       		$("#dl_books_categoryA2_0").removeClass("CategoryA");
       		$("#dl_books_categoryA2_0").removeClass("CategoryB");
   
       		$("#dl_books_categoryB3_0").removeClass("select");
       		$("#dl_books_categoryB3_0").removeClass("categoryA");
       		$("#dl_books_categoryB3_0").removeClass("categoryB");
   
       		$("#dl_books_categoryA2_0").addClass(str);
       		$("#dl_books_categoryB3_0").addClass(str);
   
           })
               .trigger('change');
       ```
   
 * =======
    CSS for the backend: ——-
 *     ```
       #dl_books_categoryA2_0			{display:none;}
       #dl_books_categoryB3_0			{display:none;}
       #dl_books_categoryA2_0.category1	{display:block;}
       #dl_books_categoryB3_0.category2	{display:block;}
       ```
   
 * This solution is very unprofessional, but I have found no other way.
    I am very
   much interested to find another solution, thanks.
 *  Thread Starter [Derek Ashauer](https://wordpress.org/support/users/sccr410/)
 * (@sccr410)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/#post-1963571)
 * What I’m looking for has nothing to do with JS or CSS, I want to populate a select
   list from items in the database. For example, I want to do a “related product”,
   so I want to pull all the products from the database to generate a select list.
 *  [BrenFM](https://wordpress.org/support/users/brenfm/)
 * (@brenfm)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/#post-1963606)
 * yes absolutely. Use the type=select and code option. I have used:
 *     ```
       [Programmes]
       type = select
       code = 0
       multiple = true
       multipleButton = true
       ```
   
 * I then put this into the PHP Code section of the plugin options (in code #0):
 *     ```
       <?php
       global $wpdb;
       $items = $wpdb->get_results("SELECT ID, post_title
       FROM $wpdb->posts
       where post_type = 'programmes'
       and post_status = 'publish'
       order by post_title ASC");
       $i = 0;
       foreach ($items as $item) {
       	$values[$i] = $item->ID;
       	$valueLabel[$i] = $item->post_title;
       	$i++;
       } ?>
       ```
   
 * Of course you just change the code = 0 line to whatever the relevant code snippet
   identifier is.
 *  Thread Starter [Derek Ashauer](https://wordpress.org/support/users/sccr410/)
 * (@sccr410)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/#post-1963627)
 * Perfect! I had no idea what code = 0 was for, there is no documentation on it.
 *  [BrenFM](https://wordpress.org/support/users/brenfm/)
 * (@brenfm)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/#post-1963649)
 * Yeah this plugin sadly is quite light on documentation, but we’ve just launched
   a HUUUGE project that uses Custom Field Templates quite extensively, so if you
   have any more issues, just sing out!
 *  Thread Starter [Derek Ashauer](https://wordpress.org/support/users/sccr410/)
 * (@sccr410)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/#post-1963654)
 * Thanks so much!
 *  [Adam W. Warner](https://wordpress.org/support/users/awarner20/)
 * (@awarner20)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/#post-1963655)
 * [@brenfm](https://wordpress.org/support/users/brenfm/),
 * I am researching this plugin to meet the specs for a client and I wonder if you
   would be so kind as to advise off forum on whether this plugin will meet our 
   needs.
 * I can be reached through the contact form [here](http://adamwwarner.com/contact).(
   not sure where to contact you)
 * If I can confirm that this plugin will work for this project (without details
   of exactly how), I can pay for any additional advisement if necessary.
 *  [BrenFM](https://wordpress.org/support/users/brenfm/)
 * (@brenfm)
 * [15 years, 2 months ago](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/#post-1963664)
 * Done! Look forward to hearing from you
 *  [pauro](https://wordpress.org/support/users/pauro/)
 * (@pauro)
 * [15 years ago](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/#post-1963727)
 * Hi,glad to found this thread
    I have tried to use code from Bren (thank you),
   and change it a bit to
 *     ```
       global $wpdb;
       $items = $wpdb->get_results("SELECT ID, post_title
       FROM $wpdb->posts
       where post_type = 'page'
       and post_status = 'publish'
       and post_parent = '60'
       order by post_title ASC");
       $i = 0;
       foreach ($items as $item) {
       	$values[$i] = $item->ID;
       	$valueLabel[$i] = $item->post_title;
       	$i++;
       }
       ```
   
 * it works nice, showing page titles from parent ’60’ in the select box. Unfortunately
   I need to show more child pages from other parent. I have tried to do it like
   this:
    `and post_parent = '60,100'` and this `and post_parent = ('60' , '100')`
   but no luck. Any suggestion?
 *  [BrenFM](https://wordpress.org/support/users/brenfm/)
 * (@brenfm)
 * [15 years ago](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/#post-1963748)
 * This is straight SQL, so what you’ll probably need to do the following (untested
   sorry, but worth a shot!):
 * `and (post_parent = '60' OR post_parent = '100')`
 * or maybe:
 * `and ((post_parent = '60') OR (post_parent = '100'))`
 * One of them should work, I would hope!
 * You could then order them by
 * `order by post_parent,post_title`
 * …if you wanted them sorted by the parent page first.
 * Hope that helps!
 *  [pauro](https://wordpress.org/support/users/pauro/)
 * (@pauro)
 * [15 years ago](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/#post-1963749)
 * Hi Bren, thank you, it helps
    For a sake of discussion, I have also tried: `and
   post_parent in (60,100,200)` and it also works (at least in my localhost). I’m
   just a beginner so my silly question is, which is better or safer?
 *  [BrenFM](https://wordpress.org/support/users/brenfm/)
 * (@brenfm)
 * [15 years ago](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/#post-1963750)
 * ahhhhh of course… IN! much easier to use in… especially if planning to expand
   your selection of parent pages. And I would think quicker (marginally) from an
   SQL point of view.
 *  [pauro](https://wordpress.org/support/users/pauro/)
 * (@pauro)
 * [15 years ago](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/#post-1963751)
 * So I assume that using “IN” is okay. I have to make sure because I am really 
   a newbie in this SQL thing and this “IN” thing I have found by the help of uncle
   google 🙂 . Hopefully this will also help others.
 * Thank you Bren.
 * Next, I want to make this dynamic select function used as Chained Select Box.
   I have start a new thread [here](http://wordpress.org/support/topic/chained-select-box?replies=1).
 * Hopefully you also can help cracking the codes.
 * thanks
 *  [Zeb](https://wordpress.org/support/users/zeb-el/)
 * (@zeb-el)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/#post-1963763)
 * I just visited this topic and tried the trick which seem to work like a charm
   and I thank you for sharing that 😀
 * However, I’ve tried to replace the post type with taxonomy in the following example
   to pull out taxonomy values for “languages” to dynamically be used in a Select
   field but it does not pull the values. I appreciate any directions in case anyone
   would help me out with this problem.
 *     ```
       global $wpdb;
       $items = $wpdb->get_results("SELECT ID, term_title
       FROM $wpdb->term
       where term= 'sprak'
       and post_status = 'publish'
       order by the_term_title ASC");
       $i = 0;
       foreach ($items as $item) {
       	$values[$i] = $item->ID;
       	$valueLabel[$i] = $item->the_term_title;
       	$i++;
       }
       ```
   
 *  [BrenFM](https://wordpress.org/support/users/brenfm/)
 * (@brenfm)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/#post-1963768)
 * Taxonomies are stored in a completely different database table (as your code 
   shows), so the idea of using “publish” won’t work as there is no field called“
   post_status” in the terms table.
 * I think you are trying to overcomplicate things. Post meta is post meta and taxonomies
   are best left well separate to this. If you are looking to associate a taxonomy
   with a post/page/custom content type then look into the Custom Post Type UI plugin.
   You can make custom post types and taxonomies with a UI and then associate the
   taxonomies with a given post type… then you’ll get a taxonomy box (like categories
   or tags in a normal post) when editing your content type.

Viewing 15 replies - 1 through 15 (of 21 total)

1 [2](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/page/2/?output_format=md)

The topic ‘[Plugin: Custom Field Template] Dynamic select list’ is closed to new
replies.

 * ![](https://ps.w.org/custom-field-template/assets/icon-256x256.png?rev=1966286)
 * [Custom Field Template](https://wordpress.org/plugins/custom-field-template/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/custom-field-template/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/custom-field-template/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-field-template/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-field-template/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-field-template/reviews/)

## Tags

 * [display](https://wordpress.org/support/topic-tag/display/)
 * [meta_key](https://wordpress.org/support/topic-tag/meta_key/)
 * [retrieve](https://wordpress.org/support/topic-tag/retrieve/)
 * [taxonomy](https://wordpress.org/support/topic-tag/taxonomy/)
 * [value](https://wordpress.org/support/topic-tag/value/)

 * 21 replies
 * 8 participants
 * Last reply from: [BrenFM](https://wordpress.org/support/users/brenfm/)
 * Last activity: [14 years, 5 months ago](https://wordpress.org/support/topic/plugin-custom-field-template-dynamic-select-list/page/2/#post-1963778)
 * Status: not resolved