Documentation

This guide is written for people like me. I am neither a programmer nor a database developer. My knowledge in CSS as in HTML is rudimentary and I do not know any PHP. Therefore, I am convinced much easier ways exist to get the intended results. All shown solutions here where written by other people. My modest contribution was to find them, test them and present them here.

I am not against using plugins. Nevertheless, I tried to get what I wanted mostly with modifications in the code. This much I can reveal: it worked. Only one additional plugin and some coding was necessary, viz. you have to alter the single.php and the function.php in your WordPress installation. Before making any alterations I strongly recommend creating a child theme. Beside all the other advantages, it is a safety net if you wreck a file.

Custom fields

In my opinion, WordPress is an extremely useful content management system. I wanted to take the term literally and manage something with it, in this case a collection of whatever. As I mentioned on the homepage I chose the data of a private collection so I will use them here as reference. I started with the entity: the work. Every work has several attributes like composer, author, cast, place, length, etc. First, this information have to be stored. The easiest solution would be creating a post for every work and write the information in the content. The clearer, smarter, and more structured way is using custom fields. They are an underestimated standard feature in WordPress. Custom fields have many advantages. In this case the bring structure into the data, viz. every data has its own field and they will all be displayed in the same order. By the way, data can be imported into custom fields – very helpful if you have a lot of information to transfer. But we will keep this for later.
Weather you use a plugin or not, if you want to see the content of the custom fields on the post you have to do some coding in the single.php. (That is the file responsible for displaying the post.) I found a lot of description how to do it. I use this one.

<div class="YOUR_CLASS_NAME">
  <?php
    $field_name = "YOUR_FIELD_NAME_1";
    $field = get_field_object($field_name);   
  ?>
  <?php
    if ($field['value'] ): ?> 
      <p id="YOUR_FIELD_ID_1"><span><?php echo $field['label'] ?>: </span><?php echo $field['value'] ?></p>
    <?php endif; ?>
  
  <?php
    $field_name = "YOUR_FIELD_NAME_2";
    $field = get_field_object($field_name);   
  ?>
  <?php
    if ($field['value'] ): ?> 
      <p id="YOUR_FIELD_ID_2"><span><?php echo $field['label'] ?>: </span><?php echo $field['value'] ?></p>
    <?php endif; ?>
</div>

Insert this code right after the closing of the meta-div. It should look like this: </div><!-- .entry-meta -->
(I sincerely hope no developer will ever stumble over this explanation!)
Although the HTML-tags are not mandatory, they help you if you want to "style" the field label or the field content with CSS.

The search criteria

Categories

Obviously, this would be a good time to fill in all the data. But if you want to find them again without a lot of searching and clicking it helps to consider the major search criteria. Therefore, I split my entity aka the work into different categories like Opera, Drama, Concert, etc. Easily done, categories are also a standard feature in WordPress. It is even possible to go more into detail, because categories are hierarchical. Take for example the category Opera. It could be further divided like

  • 17th century
  • 18th century
  • 19th century

or

  • Italian
  • French
  • German
  • Russian

Tags

The next standard feature has to be modified. To every post, an unlimited number of tags can be added, but WordPress has not a flexible tag-management actually it has none at all. Tags are not hierarchical or groupable. This means you can add the name of a composer to a work, and it will be shown on the page with all the other tags, nothing else. Now it is definitely time to use the help of a plugin. It is called Tag Groups and comes in a free version (Which will probably do all the things you need.) and in a premium version. I confess I bought it, because I wanted a little extra and the support was sensationally good. The money was well spent. As the name suggests with this plugin you organize the tags into individual groups and display them in customizable tag clouds. I only had to decide how to group them. Then I created a page for every tag group to shows its tags. Even the alphabetical tabs are done by the plugin. (Okay, I did some tiny modifications in CSS, but nothing else.) Tag Groups is simply fantastic.

If I would be easy to satisfy a little coding in the single.php and a plugin would have solved all my problems. Unfortunately, I had a very high demand.

Alphabetical order

Maybe the day will come when someone will explain to me why sorting in WordPress is so difficult. I understand it is intended for blogs, where posts are sorted according to their publishing date, but why is it so complicate to change this?

The articles

Sorting the search results by title is easy. Telling WordPress to ignore initial articles like "the", "un", "la", etc. was as impossible for me to do as to let it sort the results by a custom field. The best solution I could find was sorting by post name with this code in the function.php.

add_filter('posts_orderby', 'my_sort_custom', 50, 2);

function my_sort_custom($orderby, $query)
{
  global $wpdb;
  if (is_search()) $orderby = $wpdb->prefix . "posts.post_name ASC";
  return $orderby;
}

Majick (07.02.2016): https://wordpress.stackexchange.com/a/216942

Normally (according to the settings you made in your WordPress installation) WordPress creates as post name as soon as you give your post a title. For example, the title is "the Opera – 2019". The post name will be "the-opera-2019". Editing the name to "opera-2019" provides the correct term for sorting the post names alphabetically.
Based on this solution on can also change the sorting of the posts in the admin. Use the following code:

function custom_post_order($query){
    /* 
        Set post types.
        _builtin => true returns WordPress default post types. 
        _builtin => false returns custom registered post types. 
    */
    $post_types = get_post_types(array('_builtin' => true), 'names');
    /* The current post type. */
    $post_type = $query->get('post_type');
    /* Check post types. */
    if(in_array($post_type, $post_types)){
        /* Post Column: e.g. title */
        if($query->get('orderby') == ''){
            $query->set('orderby', 'name');
        }
        /* Post Order: ASC / DESC */
        if($query->get('order') == ''){
            $query->set('order', 'ASC');
        }
    }
}
if(is_admin()){
    add_action('pre_get_posts', 'custom_post_order');
}

Ecklund, Michael (28.09.2012): https://wordpress.stackexchange.com/a/66478

The tags again

WordPress separates tags with a comma. Extremely annoying when it comes to personal names. The tags look like this:

  • Firstname1 Lastname1,
  • Firstname2 Lastname2,

Therefore they are either sorted by the first name or one writes tags like:

  • Lastname1 Firstname1,
  • Lastname2 Firstname2,

For me an alphabetical list with names should look like this:

  • Lastname1, Firstname1
  • Lastname2, Firstname2

Putting this code in the function.php solves the problem. Tags written as "Lastname1–Firstname1" appear as "Lastname1, Firstname1".

if( !is_admin() ){
    function comma_tag_filter( $tag_arr ){
        $tag_arr_new = $tag_arr;
        if( $tag_arr->taxonomy == 'post_tag' && strpos( $tag_arr->name, '--' ) ) {
            $tag_arr_new->name = str_replace( '--', ', ', $tag_arr->name );
        }
        return $tag_arr_new;    
    }
    add_filter( 'get_post_tag', 'comma_tag_filter' );

    function comma_tags_filter( $tags_arr ) {
        $tags_arr_new = array();
        foreach( $tags_arr as $tag_arr ) {
            $tags_arr_new[] = comma_tag_filter( $tag_arr );
        }
        return $tags_arr_new;
    }
    add_filter( 'get_terms', 'comma_tags_filter' );
    add_filter( 'get_the_terms', 'comma_tags_filter' );
}

Bernhard, Andreas (14.06.2012): WordPress: tags with commas and other / custom taxonomies.

The archive

In the figurative sense, the categories are stored in the archive. So the sorting for the posts and the tags does not work there. The last addition in the function.php is a modification of this code.

add_action( 'pre_get_posts', 'custom_reverse_post_order' );
function custom_reverse_post_order( $query ) {
  if ( is_admin() )
    return;
  if ( $query->is_main_query() && is_archive() && ! is_post_type_archive() && ($query->query_vars['category_name'] == 'category-name' || $query->query_vars['category_name'] == 'category-name')  ) {
    $query->set( 'orderby', 'date' );
    $query->set( 'order', 'ASC' );
  }
}

mahodder (24.01.2014): Reverse WordPress Archive Category Order.

I wanted it to effect all categories, which is why I changed it to:

add_action( 'pre_get_posts', 'custom_reverse_post_order' );
function custom_reverse_post_order( $query ) {
  if ( is_admin() )
    return;
  if ( $query->is_archive ) {
    $query->set( 'orderby', 'name' );
    $query->set( 'order', 'ASC' );
  }
}

Additional help

Although they are not necessary, there are two plugins, which can make life easier.

ACF (Advanced Custom Fields) supports you in creating and managing custom fields. The basic version was sufficient for this website.

With WP All Import you can import XML or CSV files. The collection I used for testing has less than hundred entries. They are manageable with copy & paste. As I mentioned before this whole site is a general rehearsal for a bigger project with nearly 2.000 entries – definitely a job for this plugin.

Comments are closed.