• Hi,
    I have a bunch of movies like ‘Hugo’ ‘The Hunger Games’ ‘The Avengers’.

    At the moment I am sorting with:
    WP_Query(“category_name=Reviews&orderby=title&order=ASC”)
    But ‘The Avengers’ is after ‘Hugo’. Is there anyway I can do the sort and exclude ‘The ‘ from the starting Title?

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • You can do this by using filters to create a new sort field and use it for the orderby. Here is a pastebin with the filter functions and sample code.

    Thread Starter cyrax83

    (@cyrax83)

    Hi,
    Thanks so mcuh for your post, it almost worked, but not quite. I have put the code directly into my custom template. You can see my code here:

    https://pastebin.com/JSNiEKJL

    Here is the standard sorting result:
    https://img831.imageshack.us/img831/229/sortstd.png

    And here is using your sort function:
    https://img96.imageshack.us/img96/8290/sortfunction.png

    As you can see it’s still not quite ascending order excluding ‘The ‘

    Any ideas?

    Your template is using ‘new WP_Query()’ instead of ‘query_posts()’. Try changing this:

    $mam_global_orderby = " UPPER(sort_title) ASC";
    query_posts("posts_per_page=-1");
    $mam_global_fields = $mam_global_orderby = '';  // Turn off filters
    
        $recentPostsNew2 = new WP_Query("category_name=Reviews&orderby=sort_title&order=ASC");
        $recentPostsTitle = change_number_of_posts($recentPostsNew2);

    to this:

    $mam_global_orderby = " UPPER(sort_title) ASC";
    
        $recentPostsNew2 = new WP_Query("category_name=Reviews&orderby=sort_title&order=ASC");
    $mam_global_fields = $mam_global_orderby = '';  // Turn off filters
    
        $recentPostsTitle = change_number_of_posts($recentPostsNew2);
    Thread Starter cyrax83

    (@cyrax83)

    That worked. Fantastic. Good karma for you! ??

    If your problem has been solved, please use the dropdown on the right to mark this topic ‘Resolved’ so that anyone else with this question can see that there is a solution.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to use sort title in WP_Query and exclude 'The'’ is closed to new replies.