3

I use following function to get my custom posts ordering by title

$posts = get_posts(
    array(
        "orderby"=> "title",
        "order" => "ASC",
        "post_type" => "my-custom-post-type",
        "posts_per_page" => -1,
        "fields" => "ids",
        "meta_query" => array(
            array(
                "key" => "ams_park_id",
                "value" => get_the_ID(),
            )
        )
    )
);

And it orders by title, the problem is that i have one posts that have is titled: "It's a small world", and this is being ordered at the beginning of the list.

Example of the current returning list:

0 - "It's a small world"
1 - Albatross
2 - Alligator
3 - Baboon
4 - Camel
5 - Fox
6 - Hino
7 - Iguana
8 - Jackal
9...

How can i make to make the selector ignore the quote and send it to the "i" part of the order? Example:

0 - Albatross
1 - Alligator
2 - Baboon
3 - Camel
4 - Fox
5 - Hino
6 - Iguana
7 - "It's a small world"
8 - Jackal
9...
12
+50

Try this...

$posts = get_posts(
    array(
        "orderby"=> "slug",
        "order" => "ASC",
        "post_type" => "my-custom-post-type",
        "posts_per_page" => -1,
        "fields" => "ids",
        "meta_query" => array(
            array(
                "key" => "ams_park_id",
                "value" => get_the_ID(),
            )
        )
    )
);

Noticed I changed "orderby"=> "title", to "orderby"=> "slug". Typically the slug will be close to the title but all of the special characters will be removed.

| improve this answer | |
  • "orderby"=> "slug" didn't worked as expected, but since in posts slugs are the same as post_name, i just changed to "orderby"=> "name" and it worked like a charm. – Rodrigo Butzke Mar 10 at 19:10
0

The answer which sorts with slug in stead of title will work fine as long as you are sure the slug really correspondents with the title. Otherwise it is safer to retrieve $posts unsorted from the database and use PHP's uasort command for custom sorting. Like this:

function wpse359127_custom_sort ($post_a,$post_b) {
  // Pick the component of the post object you want to use for comparison
  $title_a = $post_a->post_title;
  $title_b = $post_b->post_title;
  // Remove any character that isn't A-Z, a-z or 0-9.
  // Or maybe do more complex things if you language has ö's, for instance
  $title_a = preg_replace("/[^A-Za-z0-9]/", '', $title_a);
  $title_b = preg_replace("/[^A-Za-z0-9]/", '', $title_b);
  // Return -1 of 1 depending on which post needs to be in front of the queue
  if ($title_a < $title_b) return -1; else return 1;
  }

uasort ($posts, 'wpse359127_custom_sort');

Note: I didn't test this code, so it might be buggy.

| improve this answer | |
0

You could duplicate the title field by hooking into save_post and store the title value (minus the offending characters) into a custom field (let’s say we name the field clean_title). In your original query you would then sort by this custom field instead of title.

Additionally you would need to write a function to be run once in which you loop over all posts and initialize the clean_title field by copying the cleaned title to it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.