admin管理员组文章数量:1394093
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.
本文标签: orderQuery posts ordering by titlebut ignore quot and special characters
版权声明:本文标题:order - Query posts ordering by title, but ignore " and special characters 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744686303a2619722.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论