重写和自定义发布类型:按自定义字段排序不起作用

时间:2015-10-30 作者:TheTrueTDF

我有一个自定义的帖子类型“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\' );

1 个回复
最合适的回答,由SO网友:Milo 整理而成

如果您的post type slug是placement, 然后你设置has_archivetrue, WordPress生成的归档文件位于/placement/. 设置has_archiveplacements 相反,让WordPress添加重写规则(这也将正确处理分页)。

然后,要按元键排序,请添加一个连接到pre_get_posts, 检查查询是否is_main_query()is_post_type_archive( \'placement\' ), 然后设置meta_query 此处的参数。

相关推荐

如何让`wp-list-table`显示我在Custom-Post中的`Custom-Fields`

一切都好吗<我需要wp-list-table 也要显示custom-fields 在每个custom-post 我有,但我不知道如何做到这一点,在这幅图中,它显示了带有字段的表格:Title, Author and Publication Date: 我想要的是能够选择custom-fields 将出现,例如以下示例Title, Carta, Naipe, Author, and Date of Publication: