我试图对页面上的帖子进行排序,但忽略了“a”、“an”和“the”。我在这一页上找到了一个很好的例子:https://css-tricks.com/ignoring-the-in-wordpress-queries/
我的查询
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => -1
);
add_filter(\'posts_fields\', \'wpcf_create_temp_column\'); // Add the temporary column filter
add_filter(\'posts_orderby\', \'wpcf_sort_by_temp_column\'); // Add the custom order filter
$query = new WP_Query( $args );
remove_filter(\'posts_fields\',\'wpcf_create_temp_column\'); // Remove the temporary column filter
remove_filter(\'posts_orderby\', \'wpcf_sort_by_temp_column\'); // Remove the temporary order filter
我的职能。php
function wpcf_create_temp_column($fields) {
global $wpdb;
$matches = \'The|A|An\';
$has_the = " CASE
WHEN $wpdb->posts.post_title regexp( \'^($matches)[[:space:]]\' )
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;
}
查询是对以“The”和“an”开头的帖子进行正确排序。不过,它并没有正确排序以“a”开头的帖子。例如,“他们自己的联盟”,就出现在“鹰”之后“派对海滩的恐怖”后面是一部短片,“合唱队”后面是“霍顿”有什么想法吗?