我有一个自定义的post类型“staff”,每个都作为一个自定义字段“\\u staff\\u purpose”(描述其功能)。
我想要档案馆的工作人员。php模板显示所有工作人员,按其\\u staff\\u目的排序,这样我就可以使用\\u staff\\u目的作为标题循环结果。我想要的结果如下:
ID | post_title | _staff_purpose
-----------------------------------------
1 | Tracy Chap. | administrator
-----------------------------------------
2 | John Doe. | teacher
-----------------------------------------
3 | Robert Smith | teacher
-----------------------------------------
原始SQL应为:
SELECT * FROM `bj_posts` p
LEFT JOIN (
SELECT post_id, meta_value as purpose
FROM `bj_postmeta` pm
WHERE `meta_key`=\'purpose\'
) pm ON p.ID=pm.post_id
WHERE p.`post_type`=\'staff\' AND p.`post_status`=\'publish\'
ORDER BY purpose ASC, post_title ASC
我试过这个:
add_action( \'pre_get_posts\', \'fetch_staff_people\' );
function fetch_staff_people( $query )
{
if ( is_page_template(\'archive-staff.php\') && $query->is_main_query() )
{
$query->set( \'post_per_page\', \'-1\' );
$query->set( \'meta_key\', \'_staff_purpose\' );
$query->set(\'orderby\', \'meta_value title\'); // sort by purpose, then by staff name.
$query->set( \'order\', \'ASC ASC\' );
} else {
return;
}
return $query;
}
查询似乎正常,但自定义字段值未显示在循环中。