自定义帖子类型‘orderby’=>‘META_VALUE’不工作

时间:2016-01-31 作者:Renshi

我有一个名为Member的自定义post类型,我已将代码添加到我的函数中。php文件为名字、姓氏和成员类型添加两个自定义列标题(这很有效),但我无法从a-Z排序默认的post\\u title列。我确信这与title vs post\\u title或其他内容有关,但这对我来说是新的。我做错了什么?

/*add custom columns to the member post type*/
add_filter( \'manage_edit-member_columns\', \'my_edit_member_columns\' );

function my_edit_member_columns( $columns ) {

    $columns = array(
        \'cb\' => \'<input type="checkbox" />\',
        \'title\' => __( \'Member\' ),
        \'first_name\' => __( \'First\' ),
        \'last_name\' => __( \'Last\' ),
        \'member_type\' => __( \'Member Type\' ),
        \'date\' => __( \'Date\' )
    );

    return $columns;
}

/*add hooks to add content to the columns*/
add_action( \'manage_member_posts_custom_column\', \'my_manage_member_columns\', 10, 2 );

function my_manage_member_columns( $column, $post_id ) {
    global $post;

    switch( $column ) {

        /* If displaying the \'first name\' column. */
        case \'first_name\' :

            /* Get the post meta. */
            $first_name = get_post_meta( $post_id, \'first_name\', true );

            /* If no first_name is found, output a default message. */
            if ( empty( $first_name ) )
                echo ( \'Unknown\' );
            else
                printf( $first_name );
            break;

        /* If displaying the \'last_name\' column. */
        case \'last_name\' :

            /* Get the post meta. */
            $last_name = get_post_meta( $post_id, \'last_name\', true );

            /* If no first_name is found, output a default message. */
            if ( empty( $last_name ) )
                echo ( \'Unknown\' );
            else
                printf( $last_name );
            break;

        /* If displaying the \'member_type\' column. */
        case \'member_type\' :

            /* Get the post meta. */
            $member_type = get_post_meta( $post_id, \'member_type\', true );

            /* If no first_name is found, output a default message. */
            if ( empty( $member_type ) )
                echo ( \'Unknown\' );
            else
                printf( $member_type );
            break;
        /* Just break out of the switch statement for everything else. */
        default :
            break;
    }
}

//make the title sortable
add_filter( \'manage_edit-member_sortable_columns\', \'my_member_sortable_columns\' );

function my_member_sortable_columns( $columns ) {

    $columns[\'post_title\'] = \'post_title\';

    return $columns;
}

// now do the sorting of the member post type
// Only run our customization on the \'edit.php\' page in the admin.
add_action( \'load-edit.php\', \'my_edit_member_load\' );

function my_edit_member_load() {
    add_filter( \'request\', \'my_sort_member\' );
}

/* Sorts the member. */
function my_sort_member( $vars ) {

    /* Check if we\'re viewing the \'member\' post type. */
    if ( isset( $vars[\'post_type\'] ) && \'member\' == $vars[\'post_type\'] ) {

        /* Check if \'orderby\' is set to \'title\'. */
        if ( isset( $vars[\'orderby\'] ) && \'post_title\' == $vars[\'orderby\'] ) {

            /* Merge the query vars with our custom variables. */
            $vars = array_merge(
                $vars,
                array(
                    \'meta_key\' => \'post_title\',
                    \'orderby\' => \'meta_value\', /*use meta_value_num if the meta_key is numeric*/
                    \'order\' => \'asc\'
                )
            );
        }
    }

    return $vars;
}

1 个回复
SO网友:Milo

帖子的标题不是元数据,而是在主帖子表中。您可以按标题订购:

\'orderby\' => \'title\'
如果字段是存储在post meta表中的自定义数据,则可以按元值指定元键/顺序。

相关推荐

sort items by randomness

我有一家电子商务商店。我是php和wordpress的新手。(虽然我有python和ruby的背景,但我不懂php)我有一个包含项目的页面。现在,最后20项显示在页面上(或类似的内容)。但我想显示的不是最后一个,而是这个类别中随机排列的项目。这是列出存档中的项目的代码。php文件:<?php echo category_description( get_category_by_slug(\'category-slug\')->term_id ); ?></div> &