1

I have a custom post type called Member I have added code to my functions.php file to add a couple of custom column headings (which is working) for First and Last name and Member Type but I can't get the default post_title column to sort from A-Z. I'm sure it has something to do with title vs post_title or something but this is new to me. What am I doing wrong?

/*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;
}
2

The post's title isn't meta data, it's in the main post table. You can order by title with:

'orderby' => 'title'

You would specify a meta key / order by meta value if the field were custom data stored in the post meta table.

|improve this answer|||||
  • Ok so I modified the code to this. array( 'post_type' => 'member', 'orderby' => 'title', 'order' => 'ASC' ) but it still is sorting by the newest post date and not by the title. – Renshi Feb 1 '16 at 2:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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