Automatically arrange items while disregarding title prefixes such as ‘the,’ ‘das,’ and ‘les’.

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPrint this page

When dealing with WordPress websites, there might be scenarios where you want to sort posts alphabetically by their titles but ignore certain prefixes like “the,” “les,” or “der.” This can be particularly useful when you want to present a clean and organized list of posts without the interference of common prefixes.

In this article, we’ll explore a custom sorting function for WordPress content based on the Advanced Post Types Order Custom Function Callback functionality. This code removes specified prefixes from post titles and then sorts them alphabetically.

function custom_sorting_function_sort_by_last_name( $posts_list, $sort_view_id, $orderBy, $query ) 
    {
        $ignored_prefixes = array( 'the', 'les', 'der' );
        $list_map = array();
        
        foreach ( $posts_list as $object_id ) {
            $object = get_post( $object_id );

            $title_parts = explode( " ", $object->post_title );

            // Remove empty spaces
            $title_parts = array_filter( $title_parts );

            if ( count( $title_parts ) > 0 ) 
            {
                $first_word = current( $title_parts );

                if ( in_array( strtolower( $first_word ), $ignored_prefixes ) ) {
                    array_shift( $title_parts ); // Remove the first part (prefix)
                }
                
                $title_without_prefix = implode( " ", array_slice( $title_parts, 1 ) );

                $list_map[ $object_id ] = $title_without_prefix;
            }
            else
            $list_map[ $object_id ] = $object->post_title;
        }

        // Sort the last names alphabetically
        asort( $list_map );

        $post_list = array_keys( $list_map );

        return $post_list;
    }

 

How to Use

Integration with Advanced Post Types Order Plugin

To use this custom sorting function along the Advanced Post Types Order. You can add the following code to your theme’s functions.php file or within a file on your /wp-content/mu-plugins/ folder.

Adjust Ignored Prefixes

Modify the $ignored_prefixes array according to your specific requirements. For example, if you want to ignore additional prefixes like “a,” “an,” or “de,” simply update the array.

$ignored_prefixes = array( 'the', 'les', 'der', 'a', 'an', 'de' );

Scenario: Sorting Books by Title

Suppose you have a custom post type for books, and the titles often start with common prefixes. By implementing this custom sorting function, you can ensure that your book list is presented alphabetically based on the actual content of the titles rather than the prefixes.

Scenario: Sorting Artists by Name

If you have a custom post type for artists or musicians, and their names are stored in the post titles, using this function can help present a clean list by sorting them alphabetically while ignoring common name prefixes.