• Resolved dapetrullo

    (@dapetrullo)


    This plugin has been very helpful, but I have posts that use articles in the title (A, An, The). Is there away for this plugin to sort these posts sans the articles? For example, “The Glamour of Hollywood” I would want sorted by “G” not “T”. I am very new to WordPress and php. Thank you for any help.

Viewing 1 replies (of 1 total)
  • Plugin Author Dani Llewellyn

    (@diddledani)

    Hi,

    You can achieve this with a bit of custom code to support your use. You will need to add something similar to the following into your theme of child-theme’s functions.php (I hope I’ve written it correctly, but do test it first if you can, because I’m not certain it’s perfect):

    function ignore_articles_in_az_index( $indices, $item ) {
        // all the articles we want to strip
        $articles = array( 'a', 'an', 'the' );
    
        // save the title for quicker access
        $title = $item->post_title;
    
        foreach( $article in $articles ) {
            // get the length of the 'article'
            $len =  mb_strlen( $article );
    
            //check whether this 'article' starts the title (+1 added to length to 
            //include space to ensure we don't match partial words like 'another'
            //would match 'an', but won't match 'an ')
            if ( mb_substr( $title, 0, $len + 1, 'UTF-8' ) === $article . ' ' ) {
                //get the true first letter to index by after the 'article'
                $index = mb_substr( $title, $len, $len + 1 );
                //return the new index - this is needlessly complex, I'll fix it one day
                return array(
                    $index => array( array(
                        'title' => $title,
                        'item' => $item,
                    ) ),
                );
            }
        }
    
        //if we get here then we didn't change the index letter so we return the
        //unmodified original to not break anything
        return $indices;
    }
    
    //hook the correct plugin-provided filter
    add_filter( 'a_z_listing_item_indices', 'ignore_articles_in_az_index', 10, 2 );

    References:
    https://developer.ww.wp.xz.cn/reference/functions/add_filter/
    https://secure.php.net/manual/en/function.mb-strlen.php
    https://secure.php.net/manual/en/function.mb-substr.php
    https://a-z-listing.com/reference/hooks/a_z_listing_item_indices/

Viewing 1 replies (of 1 total)

The topic ‘Sorting Post Titles without Articles’ is closed to new replies.