Viewing 8 replies - 1 through 8 (of 8 total)
  • I believe this plugin will help you with that:
    https://www.ads-software.com/extend/plugins/custom-field-template/

    Thread Starter MyNameis

    (@trustii)

    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!

    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>
    https://www.w3schools.com/TAGS/tag_dl.asp
    You could insert these tags in the WP HTML edit tab and style them in the CSS.

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

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

    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.

    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.

    Hi Michael

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

    Thread Starter MyNameis

    (@trustii)

    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

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to Sort Custom Fields Alphabetically By Key’ is closed to new replies.