• Resolved Noah

    (@noahsbarks)


    I use category archives as directories for viewers to find my content. They all use the same template, and therefore the same query loop. That loop is set to display in alphabetical order, but I would like it to omit initial articles like “The” and “A” when sorting.

    Does anyone know how I can do that? I tried some of the solutions I found via Google, but they seem to have no effect. I am extremely new to coding, so please explain things in a direct step-by-step fashion. I use the default twenty twenty five theme.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • I understand that you’d like to sort your category archive posts alphabetically while ignoring initial articles like “The” and “A”. Since WordPress naturally sorts by post_title including these words, a small modification to the sorting logic is required.

    Solution:

    You can add the following code to your child theme’s functions.php file or use the Code Snippets plugin if you don’t have file access:

    function custom_orderby_title_no_articles( $orderby, $query ) {
    if ( ! is_admin() && $query->is_main_query() && is_category() ) {
    global $wpdb;
    $orderby = $wpdb->prepare(
    "LOWER(
    CASE
    WHEN LEFT(post_title, %d) = %s THEN SUBSTRING(post_title, %d)
    WHEN LEFT(post_title, %d) = %s THEN SUBSTRING(post_title, %d)
    ELSE post_title
    END
    ) ASC",
    4, 'The ', 5,
    2, 'A ', 3
    );
    }
    return $orderby;
    }
    add_filter( 'posts_orderby', 'custom_orderby_title_no_articles', 10, 2 );

    How to Implement This Fix:

    Add the above code inside your child theme’s functions.php file.

    Final Steps:

    • Clear your cache (if using a caching plugin).
    • Refresh your category page (e.g., /category/music/) and check if the sorting is now ignoring “The” and “A”.

    Let me know is it fix your issue or not.

    Thread Starter Noah

    (@noahsbarks)

    Hi Sajal,

    This appears to have worked. Thank you very much!

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.