Forums

How to Sort Custom Fields Alphabetically By Key (9 posts)

  1. MyNameis
    Member
    Posted 4 years ago #

    Hi,

    I'm trying to alphabetically sort the custom fields on my post. So far, I'm only using <?php the_meta(); ?> and it just displays the fields by when I inserted them.

    Here is a link to the page I'm trying to sort. http://clicktagger.com/online-advertising-terms

    Any help is appreciated. I'm a php noob.

    Thanks!

  2. MichaelH
    Member
    Posted 4 years ago #

    I believe this plugin will help you with that:
    http://wordpress.org/extend/plugins/custom-field-template/

  3. MyNameis
    Member
    Posted 4 years ago #

    I downloaded this plugin, but I'm really not quite sure how to use it.

    How do I extract the data into a post?

    And how would I be able to sort it alphabetically?

    Sorry if I'm being an idiot.

    thanks!

  4. stvwlf
    Member
    Posted 4 years ago #

    Hi

    It doesn't happen often but in this case I think Michael's solution does not address what you want to do. I use that plugin on sites myself and was not aware it would present custom fields in any sorted order. I tested and was not able to get it to do so.

    the_meta returns an array of arrays, so what is needed is to sort the array of key values before displaying the output. I looked up the code for the_meta() and added an array sort statement. Perhaps someone more knowledgeable in WP actions/filters can explain how to do it through overriding the built-in the_meta(). In the meantime this code did what you are looking for when I tested it.

    I set it up as a function - put this in your theme's functions.php file and use <?php my_meta(); ?> in your theme template where you want its output to appear

    function my_meta() {
     if ( $keys = get_post_custom_keys() ) {
              echo "<ul class='post-meta'>\n";
              asort($keys);
              foreach ( (array) $keys as $key ) {
                  $keyt = trim($key);
                  if ( '_' == $keyt{0} )
                      continue;
                  $values = array_map('trim', get_post_custom_values($key));
                  $value = implode($values,', ');
                  echo apply_filters('the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value);
              }
              echo "</ul>\n";
          }
    }

    I wanted to ask you, by the way, why you are using custom fields for your definitions rather than just listing this as content in your post? That would be much simpler, and you could manually sort them as you add new definitions.

    HTML has a list structure specifically for key word - definition pairs - Definition List <dl>
    http://www.w3schools.com/TAGS/tag_dl.asp
    You could insert these tags in the WP HTML edit tab and style them in the CSS.

  5. MichaelH
    Member
    Posted 4 years ago #

    Sorry, I was going on this from the plugin page:

    Adds the sort option. (sort = asc, sort = desc, sort = order)

  6. stvwlf
    Member
    Posted 4 years ago #

    Hi

    I looked at the plugin again. That is a great plugin with many options. They are not well documented however. It looks like the sort is for sorting multiple data values that a single key may have rather than for sorting the keys. I don't see anywhere to indicate I want to sort the keys. Its possible it can do this and I just don't know how to configure it.

  7. MichaelH
    Member
    Posted 4 years ago #

    It looks like the sort is for sorting multiple data values that a single key may have rather than for sorting the keys. I don't see anywhere to indicate I want to sort the keys.

    You look to be correct on that Steve. Values not keys. Sorry to waste your time.

  8. stvwlf
    Member
    Posted 4 years ago #

    Hi Michael

    No problem - I got into this discussion because I wanted to learn a few things, and I did.

  9. MyNameis
    Member
    Posted 4 years ago #

    Steve,

    Many, many thanks for that. The least I can do is answer your question about why I want this in my custom fields and not the post. Well, I wanted to put a section in my sidebar where it will show a random term every time you refresh the page. I want my readers to learn new things, not just have a page to go when they need information.

    I wish I had more php knowledge to build that out on my own, but I know that if I just put the definitions into a post, then it will never happen.

    -Simon

Topic Closed

This topic has been closed to new replies.

About this Topic