Horje

As you can probably imagine by the lack of answers provided, the solution ist exactly trivial. What I've done is create a somewhat self-contained example that assumes a custom post type of " movie " and custom field key of " Genre ".

Disclaimer : this works with WP3.0 but I can be sure it will work with earlier versions.

You basically need to hook two (2) hooks to make it work and another two (2) to make it obvious and useful.

The first hook is ' restrict_manage_posts ' which lets you emit an HTML "; $html[] = " "; $this_sort = $_GET['sortby']; foreach($results as $meta_key) { $default = ($this_sort==$meta_key-> meta_key ? ' selected="selected"' : ''); $value = esc_attr($meta_key-> meta_key); $html[] = " "; } $html[] = " "; echo "Sort by: " . implode("\n",$html); } } }

The second required step is to use the parse_query hook that is called after WordPress decides one what query is should run but before it runs the query. Here we get to set values of orderby and meta_key in the query's query_var array which are documented in the Codex in the orderby parameter for query_posts() . We test to make sure that:

  1. We are in the admin ( is_admin() ),
  2. We are on the page that lists posts in the admin ( $pagenow=='edit.php' ),
  3. The page has been called with a post_type URL parameter equal to movie , and
  4. The page has also been called with a sortby URL parameter and that it wasn passed a value of ' None '

If all those tests pass we then set the query_vars (as documented here ) to meta_value and our sortby value for ' Genre ':



add_filter( 'parse_query', 'sort_movie_by_meta_value' );
function sort_movie_by_meta_value($query) { global $pagenow; if (is_admin() && $pagenow=='edit.php' && isset($_GET['post_type']) && $_GET['post_type']=='movie' && isset($_GET['sortby']) && $_GET['sortby'] !='None') { $query->
query_vars['orderby'] = 'meta_value'; $query->
query_vars['meta_key'] = $_GET['sortby']; }
}

And that's all you need to do; " posts_order " or " wp " hooks required! Of course you actually do need to do more; you need to add some columns on your page that lists the posts so you can actually see the values that it is sorting by otherwise the users will get mucho confused. So add a manage_{$post_type}_posts_columns hook, in this case manage_movie_posts_columns . This hook gets passed the default array of columns and for simplicity I just replaced it with two standard columns; a checkbox ( cb ) and a post name ( title ). (You can inspect posts_columns with a print_r() to see what else is available by default.)

I decided to add a " Sorted By: " for when there is a sortby URL parameter and when it ist None :



add_action('manage_movie_posts_columns', 'manage_movie_posts_columns');
function manage_movie_posts_columns($posts_columns) { $posts_columns = array( 'cb' =>
 $posts_columns['cb'], itle' =>
 'Movie Name', ); if (isset($_GET['sortby']) && $_GET['sortby'] !='None') $posts_columns['meta_value'] = 'Sorted By'; return $posts_columns;
}

Finally we use the manage_pages_custom_column hook to actually display the value when there is a post of the appropriate post type and with the probably redundant test for is_admin() and $pagenow=='edit.php' . When there is a sortby URL parameter we extract the custom field value that is being sorted by an display it in our list. Here's what it looks like (remember, this is test data so comments from the peanut gallery on the movie classifications! :):

Custom Columns added for a Custom Post Type in the WordPress Admin
(source: mikeschinkel.com )

And here is the code:



add_action('manage_pages_custom_column', 'manage_movie_pages_custom_column',10,2);
function manage_movie_pages_custom_column($column_name,$post_id) { global $pagenow; $post = get_post($post_id); if ($post->
post_type=='movie' && is_admin() && $pagenow=='edit.php') { switch ($column_name) { case 'meta_value': if (isset($_GET['sortby']) && $_GET['sortby'] !='None') { echo get_post_meta($post_id,$_GET['sortby'],true); } break; } }
}

Note that this only picks up the first " Genre " for a movie , i.e. the first meta_value in the case of multiple values for a given key. But then again I'mt sure how it would work otherwise!

And for those unfamiliar with where to put this code you can put it in a plugin or more likely for the newbie in the functions.php file in your current theme.

How this helps.


More Solution
Reffered: https://wordpress.org/