Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to order by post meta name which I added for post. Post meta is "_merchant_id" an I know how to sort by merchan_id but I don't know how to sort by merchant name.

I am sorting by merchant id like this:

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;
...

Merchant is another post type ("merchant"). How can I sort by merchant name?

UPDATE (improved explanation by screenshot):

enter image description here

share|improve this question
    
Did you read this? It was similar, maybe even more difficult. :) –  toscho Dec 17 '12 at 22:50
    
@toscho - Tried. But I get default posts, not my custom post type "Deals" –  CroiOS Dec 18 '12 at 11:11
    
@toscho - This works. I don't know the rules here at stackoverflow so I'm not sure if I can copy this answer so I can award you with "bounty"? I solved this issue so the other users in the future might find this helpful. –  CroiOS Dec 18 '12 at 13:50
2  
Add your specific solution as an answer. It is probably not exactly the same as the linked answer. The bounty will be lost. –  toscho Dec 18 '12 at 13:53
add comment

3 Answers

I assume merchant name is another meta field and not the title for that post type? If so, here is a way to organize your admin edit.php area

// 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;
}
share|improve this answer
    
Kristina, I have updated explanation by adding screenshoot. Please take a look at it. –  CroiOS Dec 14 '12 at 9:44
    
Post titles are supposed to be sortable by default... you shouldn't have to do anything special to make that work. Mind posting your cpt/taxonomy registration code? –  Kristina Childs Dec 14 '12 at 17:34
    
I have not code because this is in some plugin implemented. I have deals (post type) which have saved in postmeta their merchant id and then over that merhcant id i connect to merchant (post type). How can I in wp admin deal printing page sort (DESC/ASC) by merchant's name? –  CroiOS Dec 14 '12 at 21:05
1  
The callbacks and functions names don't match in the first two hooks... –  brasofilo Dec 17 '12 at 19:34
1  
Or there's that. Sorry, I modified from one of my existing wordpress sites and must have missed that. Thanks @brasofilo. @CroiOS, try the code again now that I've fixed that typo. (using post_title) instead of merchant_type) –  Kristina Childs Dec 17 '12 at 22:44
show 4 more comments

The meta values and names are not useable in admin area on default.

But you can enahnce the query to use it in the loop, the query like the example in your question. To add the meta values use the follow small snippet in plugin.

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;
}

A other alternative is to hook in the query and change the query directly.

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';
    }
}
share|improve this answer
1  
Merchant name (the bit he wants to sort) isn't a meta key, it's the post title. It's odd that the plugin he uses would disable that function. –  Kristina Childs Dec 19 '12 at 23:03
add comment
up vote 0 down vote accepted

I hope that my solution will be useful to somebody.

 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;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.