WordPress.org

Ready to get started?Download WordPress

Forums

Custom Field Template
Dynamic select list (22 posts)

  1. sccr410
    Member
    Posted 3 years ago #

    Is there a way using Custom Field Template to populate a select list dynamically?

  2. daveleeone
    Member
    Posted 3 years ago #

    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.

  3. sccr410
    Member
    Posted 3 years ago #

    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.

  4. BrenFM
    Member
    Posted 3 years ago #

    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.

  5. sccr410
    Member
    Posted 3 years ago #

    Perfect! I had no idea what code = 0 was for, there is no documentation on it.

  6. BrenFM
    Member
    Posted 3 years ago #

    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!

  7. sccr410
    Member
    Posted 3 years ago #

    Thanks so much!

  8. Adam W. Warner
    Member
    Posted 3 years ago #

    @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. (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.

  9. BrenFM
    Member
    Posted 3 years ago #

    Done! Look forward to hearing from you

  10. pauro
    Member
    Posted 3 years ago #

    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?

  11. BrenFM
    Member
    Posted 3 years ago #

    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!

  12. pauro
    Member
    Posted 3 years ago #

    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?

  13. BrenFM
    Member
    Posted 3 years ago #

    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.

  14. pauro
    Member
    Posted 3 years ago #

    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.

    Hopefully you also can help cracking the codes.

    thanks

  15. Zeb
    Member
    Posted 2 years ago #

    I just visited this topic and tried the trick which seem to work like a charm and I thank you for sharing that :D

    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++;
    }
  16. BrenFM
    Member
    Posted 2 years ago #

    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.

  17. Zeb
    Member
    Posted 2 years ago #

    Thanks for your opinion @BrenFM. I have already used that but I can't get what I'm intend to do.

    Well, my intention is to put some data in a custom post type called services with small thumbs (icons) and then give the user capability to choose from these alternatives. However, when a user checks some of the check boxes, it is intended that the thumbs to be published in a row and the value should be displayed as tooltip.

    Of course, this could be solved by calling the values into the custom field (as discussed earlier in this post) but I can not get the value or the thumb to be published.

    For example, The values of the custom post type appears in the custom field (check boxes and select box) but when I intent to retrieve the value, I get only the ID's of the post types and not the values. Meanwhile, even empty values appearing with list buttons without any value.

    Is there any way to retrieve the thumb, value and the key by any code and at the same let the empty values to be ignored?

    Please, anyone!
    Any help is appreciated.

  18. rfrancis
    Member
    Posted 2 years ago #

    First, Bren - thanks this is great, but I'm getting the following error:

    Parse error: syntax error, unexpected '<' in ***/wp-content/plugins/custom-field-template/custom-field-template.php(1779) : eval()'d code on line 1

    Note, the *** represents the server path.

    I checked line 1779 in the plugin file, and it appears to be dealing with cleansing the content going into the values for the select... ?

    I appreciate any ideas.

  19. rfrancis
    Member
    Posted 2 years ago #

    Never mind, I got it... big DOH! Had to remove the <?php and ?>

  20. trip_levine
    Member
    Posted 2 years ago #

    I'm going to confirm this plugin is a waste of your time. The only way it works is if you hand-code 90% of its functionality from scratch. There are no working resources online to guide you through the process. If you are capable of coding the 90% yourself, than you mind as well do it entirely yourself.

    tl;dr; This plugin is a waste of WordPress hosting space on their plugins page.

  21. Zeb
    Member
    Posted 2 years ago #

    I agree that this plugin is a waste of time!

    It looks ok in the back-end but it needs lots of attention. If you want to move the code to a new installation, it works good too.

    When it comes to presenting the information in the front-end where all visitors can see... uhhhh!

    I worked a couple of weeks on this one to get it work without luck. This plugin is a nightmare. I can not realize what the author had intended to do with this. He thought maybe to make something that is not easy to implement ... it is easier to hard code custom fields by yourself!

  22. BrenFM
    Member
    Posted 2 years ago #

    Hay Zeb.

    Sorry - I'm not sure I've ever read your post til now. I really don't understand what you are trying to do (though I fear it's probably a bit late some three months down the track).

    I'm not the developer of this plugin (clearly) but more than happy to help out anyone that is having issues.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic