我有一个自定义的帖子类型“placement”,带有一个自定义字段“position”。我想做的是,在placements archive页面上,按“position”值对结果进行排序。
我有以下URL重写规则:
$newRules[\'placements/?$\'] = \'index.php?post_type=placement&meta_key=position&orderby=meta_value&order=DESC\';
以下是如何定义我的自定义帖子类型:
function custom_post_type_placement() {
$labels = array(
\'name\' => _x( \'Placements\', \'Post Type General Name\', \'text_domain\' ),
\'singular_name\' => _x( \'Placement\', \'Post Type Singular Name\', \'text_domain\' ),
\'menu_name\' => __( \'Placement\', \'text_domain\' ),
\'name_admin_bar\' => __( \'Placement\', \'text_domain\' ),
\'parent_item_colon\' => __( \'Parent Placement:\', \'text_domain\' ),
\'all_items\' => __( \'All Placements\', \'text_domain\' ),
\'add_new_item\' => __( \'Add New Placement\', \'text_domain\' ),
\'add_new\' => __( \'Add New\', \'text_domain\' ),
\'new_item\' => __( \'New Placement\', \'text_domain\' ),
\'edit_item\' => __( \'Edit Placement\', \'text_domain\' ),
\'update_item\' => __( \'Update Placement\', \'text_domain\' ),
\'view_item\' => __( \'View Placement\', \'text_domain\' ),
\'search_items\' => __( \'Search Placement\', \'text_domain\' ),
\'not_found\' => __( \'Not found\', \'text_domain\' ),
\'not_found_in_trash\' => __( \'Not found in Trash\', \'text_domain\' ),
);
$args = array(
\'label\' => __( \'Placement\', \'text_domain\' ),
\'description\' => __( \'Placements\', \'text_domain\' ),
\'labels\' => $labels,
\'supports\' => array( \'title\', \'editor\', ),
\'taxonomies\' => array( \'journal_placement\' ),
\'hierarchical\' => false,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'menu_position\' => 5,
\'show_in_admin_bar\' => true,
\'show_in_nav_menus\' => true,
\'can_export\' => true,
\'has_archive\' => true,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'capability_type\' => \'page\',
);
register_post_type( \'placement\', $args );
add_action( \'init\', \'custom_post_type_placement\', 0 );
我能够正确地切换ASC和DESC,但我的结果仍按发布日期排序。
以下是查询的内容:
SELECT SQL_CALC_FOUND_ROWS fetr_posts.ID FROM fetr_posts WHERE 1=1 AND fetr_posts.post_type = \'placement\' AND (fetr_posts.post_status = \'publish\' OR fetr_posts.post_status = \'private\') ORDER BY fetr_posts.post_date DESC LIMIT 0, 10
似乎没有考虑meta\\u键和orderby参数。你知道为什么以及如何使用重写规则对结果进行正确排序吗?
编辑:多亏了米洛的回答,我按以下方式更改了代码,现在可以正常工作了。
我完全删除了重写规则,并将我的自定义帖子定义如下:
function custom_post_type_placement() {
$labels = array(
\'name\' => _x( \'Placements\', \'Post Type General Name\', \'text_domain\' ),
\'singular_name\' => _x( \'Placement\', \'Post Type Singular Name\', \'text_domain\' ),
\'menu_name\' => __( \'Placement\', \'text_domain\' ),
\'name_admin_bar\' => __( \'Placement\', \'text_domain\' ),
\'parent_item_colon\' => __( \'Parent Placement:\', \'text_domain\' ),
\'all_items\' => __( \'All Placements\', \'text_domain\' ),
\'add_new_item\' => __( \'Add New Placement\', \'text_domain\' ),
\'add_new\' => __( \'Add New\', \'text_domain\' ),
\'new_item\' => __( \'New Placement\', \'text_domain\' ),
\'edit_item\' => __( \'Edit Placement\', \'text_domain\' ),
\'update_item\' => __( \'Update Placement\', \'text_domain\' ),
\'view_item\' => __( \'View Placement\', \'text_domain\' ),
\'search_items\' => __( \'Search Placement\', \'text_domain\' ),
\'not_found\' => __( \'Not found\', \'text_domain\' ),
\'not_found_in_trash\' => __( \'Not found in Trash\', \'text_domain\' ),
);
$rewrite = array(
\'slug\' => \'placements\',
\'with_front\' => false,
\'pages\' => true,
\'feeds\' => false,
);
$args = array(
\'label\' => __( \'Placement\', \'text_domain\' ),
\'description\' => __( \'Placements\', \'text_domain\' ),
\'labels\' => $labels,
\'supports\' => array( \'title\', \'editor\', ),
\'taxonomies\' => array( \'journal_placement\' ),
\'hierarchical\' => false,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'menu_position\' => 5,
\'show_in_admin_bar\' => true,
\'show_in_nav_menus\' => true,
\'can_export\' => true,
\'has_archive\' => true,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'rewrite\' => $rewrite,
\'capability_type\' => \'page\',
);
register_post_type( \'placement\', $args );
}
add_action( \'init\', \'custom_post_type_placement\', 0 );
我还添加了此功能:
function order_by_position($query){
if(is_post_type_archive( \'placement\' )){
$query->query_vars[\'meta_key\']="position";
$query->query_vars[\'orderby\']="meta_value_num";
}
return $query;
}
add_action( \'pre_get_posts\', \'order_by_position\' );