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...
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...
Share
Improve this question
edited Mar 5, 2020 at 14:13
RiddleMeThis
3,8078 gold badges22 silver badges30 bronze badges
asked Feb 20, 2020 at 18:34
Rodrigo ButzkeRodrigo Butzke
17513 bronze badges
3 Answers
Reset to default 13 +50Try 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.
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.
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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744686303a4587939.html
评论列表(0条)