• I need my posts to display in alphabetical order, but ignoring initial articles like “A” and “The”. I was able to implement the solution found at https://css-tricks.com/ignoring-the-in-wordpress-queries/ by adding the code (including the filters) to my functions.php child theme file.

    This worked great, but also overrode a couple of plugins:
    1. A related posts plugin that sorted by relevance
    2. A featured posts plugin that randomized posts

    According the the CSS Tricks article, I need to move the filter calls to the specific places where I want the customized sort to happen. The problem is I’m not savvy enough to figure out where. I know enough to copy and paste code to where I’m directed to, but not enough to customize existing functions to accommodate new code.

    This is the code I’m using:

    function wpcf_create_temp_column($fields) {
      global $wpdb;
      $matches = 'A|An|The';
      $has_the = " CASE 
          WHEN $wpdb->posts.post_title regexp( '^($matches)<a href="https://codex.wordpress.org/:space:">:space:</a>' )
            THEN trim(substr($wpdb->posts.post_title from 4)) 
          ELSE $wpdb->posts.post_title 
            END AS title2";
      if ($has_the) {
        $fields .= ( preg_match( '/^(\s+)?,/', $has_the ) ) ? $has_the : ", $has_the";
      }
      return $fields;
    }
    
    function wpcf_sort_by_temp_column ($orderby) {
      $custom_orderby = " UPPER(title2) ASC";
      if ($custom_orderby) {
        $orderby = $custom_orderby;
      }
      return $orderby;
    }
    add_filter('posts_fields', 'wpcf_create_temp_column');
    add_filter('posts_orderby', 'wpcf_sort_by_temp_column');

    Is there a way to make this not affect plugins?

    Alternatively if there’s a way to tell WordPress to sort by slug instead of title, that would work too. It just needs to not affect those two plugins. I’ve read tons of different posts about sorting by slug, but they involve specific functions that coders are working on for their custom themes. I need something beginner-friendly that I can copy and paste. Does such a solution exist?

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Sorting by slug would be much more efficient than trying to ignore articles. For WP_Query, pass 'orderby' => 'name',. In SQL, do something like ORDER BY $wpdb->posts.post_name ASC. Generally speaking, in WP, name == slug. Why the discrepancy? IDK

    As for avoiding plugin conflicts, you could identify something unique about your query that’s distinct from the plugin’s queries. Use that as a conditional check before doing anything. Or as you suggest, add your filter callback as late as possible, just before the query is made. For example, by adding from a custom template. Once your callback has done its thing, it can remove itself from the filter call stack so it would not affect subsequent queries.

    Thread Starter packdragon

    (@packdragon)

    Thank you for your help. I think this is probably beyond the scope of my abilities, and I’ll probably have to hire a developer to figure it out for me.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Make sorting ignore “the” OR sort by slug’ is closed to new replies.