Support » Developing with WordPress » convert taxonomy plugin to custom field plugin

  • Hi I have the following plugin to create a select dropdown list of taxonomy terms:

    * Plugin Name: Select plant by taxonomy tag term
    * Description: Create select dropdown list of taxonomy terms, select term to open plant details page. 
    *              Select box uses jQuery Chosen plugin. 
    * Version: 1.0 
    * Author: David Myers
    function select_post_by_tax_tag($atts) {
              'list_name' => 1, ), $atts));
        $args = array(
            'orderby'       => 'name',
            'order'         => 'ASC',
            'hide_empty'    => 1,
            'taxonomy'      => $list_name, //change this to any taxonomy
            $select_box = '';
        foreach (get_categories($args) as $tax) :  
            $args = array(
                'post_type'         => 'plant', //change to your post_type
                'posts_per_page'    => 1,
                'orderby'           => 'title',
                'order'             => 'ASC',
                'tax_query' => array(
                        'taxonomy'  => $list_name, //change this to any taxonomy
                        'field'     => 'slug',
                        'terms'     => $tax->slug
            if (get_posts($args)) :
            foreach(get_posts($args) as $p) : 
    		if (strlen($tax->name) > 2) 
     $select_box.='<option value="' . get_permalink($p) . '">' . $tax->name . '</option>';
    	return $select_box;
    add_shortcode('post_by_eng_name', 'select_post_by_tax_tag');

    I now need a similar plugin for “custom fields” instead of taxonomy.
    I think I need to use get_post_meta instead of get_categories, I’ve been struggling with this for some time without any success.
    Any help much appreciated.

Viewing 9 replies - 1 through 9 (of 9 total)
  • When in doubt, read plugins that are already written. 🙂

    That’s how I arrived to the plugin above, but now I need the same for custom fields and can’t find anything that works for me – do you have any suggestions where I might find such a plugin?


    Well, what you posted is out of context so it’s difficult to tell what you are doing.
    If you read about how to use custom fields in the documentation, you will see how to set them and how to retrieve them.

    Thanks Joy, you’re right, I’m just not finding it easy to work this out, which is why I’m here. The plugin retrieves taxonomies and presents them in a <select> list, I just need to do the same with custom fields or alternatively change the custom fields to taxonomies.

    I’m surprised your function works since you are reusing the $args variable in the two loops that both use it in the loop condition. And it is a little confusing to call it ‘list_name’, but give a number as the default.

    You say you are retrieving taxonomies, but aren’t you actually getting terms? Or is it post links with those terms? Custom fields work a little differently. Each post can have the custom field or not, just like it can have a term or not, but there is no function to call to get the list of all the ones used on all the posts, like there is for terms.

    Hi Joy,
    I’m a WordPress/PHP novice and this plugin was copy/pasted from different sources which shows in the logic as you pointed out – so really appreciate your input.
    As I understand a taxonomy is part of a hierarchical structure and a custom field is a flat piece of data, but I’m not clear what’s meant by term and why a taxonomy can have a term and a custom field can’t – which means I can’t loop thru posts to retrieve the custom fields.
    Would changing the custom fields to taxonomies be a reasonable solution?
    Thanks for your time

    Moderator bcworkz


    A taxonomy is not part of a hierarchical structure, it IS the hierarchical structure. A structure of terms that can be assigned to posts. I’m assuming your “plant” post type refers to the flowers, shrubs and trees sort of plants and not manufacturing plants. The living organism classification system of order, family, genus, species, etc. is itself a taxonomy. The names assigned for a specific plant, such as Acer macrophyllum are taxonomy terms. I suggest you study the Taxonomies Codex article for further information.

    Meta data does not have terms because it’s organized differently. Taxonomies are organized with terms, meta data (or Custom Fields, same thing, different label) is organized by keys and values. They are intentionally organized differently to meet different needs. If they were organized the same, there would be no point in having different data structures.

    Joy is correct in that there is no function to call to get all meta data. However, that doe not mean one could not code something to loop through posts and get related meta data. However, you could likely get meta data used by your theme or other plugins, or core WP itself which have no bearing on what you are trying to do. It’s better to get meta data values related to specific keys that you have an interest in. In any case, the process gets computationally very expensive. Meta data is better when you know what you want to get. Trying to get anything related to a post can get messy.

    Without knowing the nature of the meta data, it’s difficult to say if the same can be implemented as a taxonomy. Given your need to collect all values assigned to posts, this is much easier for taxonomy terms than meta data, so I’ll answer with a very tentative yes, taxonomies are possibly better for you.

    Hi, Thanks for the detailed explanation, yes I’m creating a site for the medicinal use of middle eastern botanical plants.
    Each plant has a botanical, english, arabic and hebrew name – all custom fields. I want to create 4 dropdown lists, selecting a plant from any of these lists will link to the post for the medicinal uses of that plant. I wasn’t sure how to create the taxonomy in wordpress and also how to upload the taxonomy structure from a csv file, hence they’re custom fields. I’d be happy to hear any suggestions on how to organize this data into a hierarchical structure.


    Moderator bcworkz


    Oooo! That sounds interesting, to me at least 🙂

    Names for specific plants makes more sense as custom fields than taxonomy terms. Because there is basically one value per plant. One to one relations. For genus and specie at least. The family might be better as a taxonomy. The medicinal use (diuretic, anti-emetic, etc.) makes more sense as taxonomy terms. Possibly multiple terms per plant and multiple plants per term. Many to many relations.

    I question your UI design though. Presenting a long list of names in dropdowns makes for a poor user experience. Why not simply present alphabetical index pages? They are long lists either way, but a multi-column list on a page is much easier to scan than a dropdown list. My 2 cents worth, but it’s your site. Do as you wish. Generating the list is conceptually the same either way, only the type of output is different.

    As I mentioned earlier, you can get an array of plant posts, then loop through the array and collect their associated meta data. While this is straight forward to code, it’s computationally expensive (lots of queries). A custom SQL query that gets all required data in one query without extraneous unneeded data would be much more efficient. Depending on the size of your data, the easy to code approach may be fine. It doesn’t scale up very well, but it may suffice for your needs.

    To import CSV data, whether for custom fields or taxonomy terms, first peruse available CSV import plugins. They tend to be for importing posts, but it’s worth a look. The alternative if nothing can be found is custom coding an import script. Similar to outputting a name list, you loop through the CSV list and assign values to the related plant posts using normal WP PHP functions. What complicates things is your script will likely time out before it can get through the entire list, so the import may need to be managed in chunks. The chunking can be coded as well, but it may be easier to manually break up the CSV source into manageable chunks.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘convert taxonomy plugin to custom field plugin’ is closed to new replies.