如何在wp管理中通过发布元名称进行订购?

时间:2012-12-13 作者:iWizard

我试图通过我为帖子添加的帖子元名称进行订购。Post meta是“\\u merchant\\u id”,我知道如何按merchant\\u id排序,但我不知道如何按商户名称排序。

我按商户id分类如下:

public function column_orderby( $vars ) {
        if ( isset( $vars[\'orderby\'] ) && is_admin() ) {
            switch ( $vars[\'orderby\'] ) {
case \'merchant\':
 $vars = array_merge( $vars, array(
     \'meta_key\' => \'_merchant_id\',
     \'orderby\' => \'meta_value_num\'
 ) );

break;
...
商户是另一种邮政类型(“商户”)。如何按商户名称排序?

UPDATE (improved explanation by screenshot):

enter image description here

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

我希望我的解决方案对某人有用。

 public function column_orderby( $vars ) {


        if ( isset( $vars[\'orderby\'] ) && is_admin() ) {
            switch ( $vars[\'orderby\'] ) {

            ...

            case \'merchant\':


                GLOBAL $wpdb, $query;


                $vars = array_merge( 
                    $vars, array(
                    \'meta_key\' => \'_merchant_id\',
                    \'orderby\' => \'meta_value_num\'
                    ) 
                );

                add_filter(\'posts_clauses\', \'e_order_by_mechant_name\',10,2);


                break;

            ...
        }

        return $vars;
}


function e_order_by_mechant_name( $clauses, $query ) {
    global $wpdb;
    if ( ! $query->is_main_query()
        || ! is_admin()

    ){
        return $clauses;
    }

    //Get sort order
    $order_dir = $query->get(\'order\');
    $order_dir = (\'asc\' == $order_dir ? \'ASC\' : \'DESC\');

    //Join user table onto the postmeta table
    $clauses[\'join\'] .= " LEFT JOIN {$wpdb->prefix}posts merchants ON {$wpdb->prefix}postmeta.meta_value = merchants.ID";

    //Replace orderby
    $clauses[\'orderby\']  = " merchants.post_title $order_dir";

    return $clauses;
}

SO网友:kristina childs

我假设商户名称是另一个元字段,而不是该帖子类型的标题?如果是这样的话,这里有一种方法可以组织您的管理编辑。php区域

// Add a column in admin edit.php to display the Merchant post type data you want shown
add_filter(\'manage_merchant_posts_columns\', \'admin_merchant_columns\');
function admin_merchant_columns( $posts_columns ) {
    $posts_columns = array(
        \'cb\' => \'<input type="checkbox" />\', // the checkbox to select the line item
        \'title\' => __( \'Name\' ), // post title
        \'merchant_name\' => __( \'Merchant Name\' ), // where merchant_name is your meta key for that field
        \'_merchant_id\' => __( \'Merchant ID\' ) // merchant id meta key
    );

    return $posts_columns;
}

// Fill the column with the appropriate items
add_action( \'manage_merchant_posts_custom_column\', \'manage_merchant_columns\', 10, 2 );
function manage_merchant_columns( $column, $post_id ) {
    global $post;
    switch( $column ) {
        case \'merchant_name\' :
            $merchant_name = get_post_meta( $post_id, \'merchant_name\'); 
            if ( empty( $merchant_name ) )
                echo ( \'\' );
            else
                print join( $merchant_name, \', \' );

            break;
        case \'_merchant_id\' :
            $_merchant_id = get_post_meta( $post_id, \'_merchant_id\'); 
            if ( empty( $_merchant_id ) )
                echo ( \'\' );
            else
                print join( $_merchant_id, \', \' );

            break;

        default :
            break;
    }
}

// add ability to sort by merchant name
add_filter( \'manage_edit-merchant_sortable_columns\', \'sort_by_merchant_name\' );
function sort_by_merchant_name( $columns ) {
    $columns[\'merchant_name\'] = \'merchant_name\';
    return $columns;
}

add_action( \'load-edit.php\', \'sort_by_merchant_name_load\' );
function sort_by_merchant_name_load() {
    add_filter( \'request\', \'sort_merchant\' ); // where "merchant" is your custom post type slug
}

function sort_merchants( $vars ) {
    if ( isset( $vars[\'post_type\'] ) && \'merchant\' == $vars[\'post_type\'] ) { // where "merchant" is your custom post type slug
        if ( isset( $vars[\'orderby\'] ) && \'merchant_name\' == $vars[\'orderby\'] ) {
            $vars = array_merge(
                $vars,
                array(
                    \'meta_key\' => \'merchant_name\',
                    \'orderby\' => \'meta_value\'
                )
            );
        }
    }
    return $vars;
}

SO网友:bueltge

默认情况下,元值和名称在管理区域中不可用。

但您可以启用查询以在循环中使用它,查询如您问题中的示例所示。要添加元值,请使用插件中的以下小片段。

add_filter( \'query_vars\', \'fb_query_vars_admin\' );
/**
 * If needed: Allow meta queries in the admin
 */
function fb_query_vars_admin( $query_vars ) {

    // break off, if not in admin area
    if ( ! is_admin() )
        return $query_vars;

    $query_vars[] = \'meta_key\'; // my key of custom field
    $query_vars[] = \'meta_value\'; // my value of custom field

    return $query_vars;
}
另一种选择是挂接查询并直接更改查询。

add_filter( \'parse_query\', \'fb_custom_post_sort\' );
function fb_custom_post_sort($query) {

    if ( ! is_admin() )
        return $query;

    global $current_screen;
    if ( isset( $current_screen ) && \'post\' === $current_screen->post_type ) {
        $query->query_vars[\'orderby\']  = \'meta_value\';
        $query->query_vars[\'meta_key\'] = \'_merchant_id\';
        $query->query_vars[\'order\']    = \'ASC\';
    }
}

结束

相关推荐

List latest posts in WP-Admin

我运行了一个多作者网站,并在wp admin中创建了一个插件,作者可以在其中相互连接和协作。为了增强它,我想显示一个最新帖子的列表,该列表应如下所示:如图所示,该列表应包含最新的10篇帖子,其中包含用户名、链接的帖子标题以及发布时间(发布时间或发布时间)。用于此的HTML应为:<table class=\"widefat\"> <thead><tr><th scope=\"col\">User</th>&