好吧,我想展示一下我在按类别/分类法对自定义帖子类型进行排序方面的经验。
THE WEB
<一个在WordPress上运行的旅行社网站,主要内容是关于名为“ruta”的自定义帖子类型的分类法,该结构类型为旅行>大陆>国家
THE CASE
在归档类别列表页面中,客户希望帖子按
大陆,按每条路线的数量排序国家,按字母顺序排列
THE STEPS
First, 我从未修改的归档页查询中捕获到请求,该查询碰巧是这样的:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE 1=1
AND ( wp_term_relationships.term_taxonomy_id IN (5,6,7,8,9,10,11,12,13,15,16,17,18,19,20,21,22,23,25,26,28,29,31,32,33,35,38,95,101,102,193) )
AND wp_posts.post_type IN (\'ruta\', \'nav_menu_item\')
AND (wp_posts.post_status = \'publish\' OR wp_posts.post_author = 45
AND wp_posts.post_status = \'private\')
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC LIMIT 0, 20
Second, 我在Sequel Pro中根据数据库编辑了sql代码,以符合我的需要。我得出这样的结论(是的,也许可以改进:我在MySQL方面的知识并不出众):
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID, tt1.parent AS pare,
(
SELECT COUNT(*)
FROM wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_taxonomy AS tt1 ON ( tt1.term_taxonomy_id = wp_term_relationships.term_taxonomy_id )
INNER JOIN wp_term_taxonomy AS tt2 ON ( tt2.term_taxonomy_id = tt1.term_taxonomy_id )
WHERE 1=1
AND tt1.parent = pare
) AS Total
FROM wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_taxonomy AS tt1 ON ( tt1.term_taxonomy_id = wp_term_relationships.term_taxonomy_id )
INNER JOIN wp_terms ON ( tt1.term_id = wp_terms.term_id )
WHERE 1=1
AND ( wp_term_relationships.term_taxonomy_id IN (5,6,7,8,9,10,11,12,13,15,16,17,18,19,20,21,22,23,25,26,28,29,31,32,33,35,38,95,101,102,193) )
AND wp_posts.post_type IN (\'ruta\', \'nav_menu_item\')
AND (wp_posts.post_status = \'publish\' OR wp_posts.post_author = 45
AND wp_posts.post_status = \'private\')
GROUP BY wp_posts.ID
ORDER BY
total DESC,
wp_terms.name
Third, 我将查询挂接到函数上。带有三个过滤器的php文件:posts\\u字段、posts\\u join和posts\\u orderby
函数中的代码。php:
function xc_query_fields( $fields ) {
$fields = "wp_posts.ID, wp_posts.post_title, wp_terms.name, tt1.parent AS pare,
(
SELECT COUNT(*)
FROM wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_taxonomy AS tt1 ON ( tt1.term_taxonomy_id = wp_term_relationships.term_taxonomy_id )
INNER JOIN wp_term_taxonomy AS tt2 ON ( tt2.term_taxonomy_id = tt1.term_taxonomy_id )
WHERE 1=1
AND tt1.parent = pare
)
AS Total";
return $fields;
}
function xc_query_joins( $join ) {
$join .= "INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_taxonomy AS tt1 ON ( tt1.term_taxonomy_id = wp_term_relationships.term_taxonomy_id )
INNER JOIN wp_terms ON ( tt1.term_id = wp_terms.term_id )";
return $join;
}
function xc_query_orderby( $join ) {
$join = "total DESC, wp_terms.name ";
return $join;
}
Finally 根据某些条件,我从pre\\u get\\u post挂钩触发了过滤器
function filtra_queries( $query )
{
if ( is_archive() && $query->is_main_query() && !is_admin() ) {
$rutes = array(\'viajes-privados\', \'asia\', \'africa\', \'oceania\', \'america\', \'oriente-proximo\');
if ( in_array( $query->get(\'category_name\'), $rutes ) )
{
add_filter( \'posts_fields\', \'xc_query_fields\' );
add_filter( \'posts_join\', \'xc_query_joins\' );
add_filter( \'posts_orderby\', \'xc_query_orderby\' );
}// end if in_array
}// end if is_archive
}
add_filter(\'pre_get_posts\', \'filtra_queries\');
希望这能帮助别人