WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on a pretty complicated site where everything is managed by category type including automatic spinner image submission, etc.. Unfortunately, its doggone difficult to manage things when the admin panel "All Posts" sorts them by date (DESC) I'm thinking it sure would be handy to have the show "All Posts" in category name order.

I Found this small routine, that works for sorting by Post name.

 /*
 Plugin Name: Set Post Order In Admin
 Version: 0.1
 Plugin URI: http://wordpress.org/support/topic/336715/
 Description: In Posts->Edit, display posts in title order.
 Author: MichaelH
 Author URI: http://codex.wordpress.org/User:MichaelH/MyPlugins
 check out: http://codex.wordpress.org/Class_Reference/WP_Query
 in the section order and orderby parameters  */
 function set_post_order_in_admin( $wp_query ) {
    if ( is_admin() ) {  
         $wp_query->set( 'orderby', 'name' );
         $wp_query->set( 'order', 'ASC' );  /* ASC or DESC */
    }
 }
 add_filter('pre_get_posts', 'set_post_order_in_admin' );

That works okay for sorting by title, but I really need to be able to sort by category name. I tried playing with the $wp_query->set( 'meta_key', 'category' ); and $wp_query->set( 'orderby', 'meta_value' ); but when I started using those, I just got an empty return for All Posts within my admin panel. Any ideas on what is missing? I've tried a number of terms for meta_key all of them fail. Anybody been here before? Any idea on how to achieve the goal of sorting All posts in the admin panel by category (parent)??

share|improve this question

zip, you might need to save your category as a new post meta first, that's how orderby meta_value works.

Not sure if this could help, I stumbled upon this plugin: http://wordpress.org/plugins/custom-post-order-category/, it could let you specified a category then order posts in that category.

If you're curious about how to save category into a post meta, I've used the Meta Box plugin to do so: http://wordpress.org/plugins/meta-box/. Let me know if you need further assistance.

share|improve this answer

Putting aside the objections to order by taxonomy, eloquently given by @Otto in http://wordpress.stackexchange.com/a/14309/25886, I suppose his answer has merit.

His reference to http://scribu.net/wordpress/sortable-taxonomy-columns.html in particular, with a post on sortable taxonomy columns.

The folllowing code is by @mikeschinkel:

function color_clauses_mike( $clauses, $wp_query ) {
    global $wpdb;

    if ( isset( $wp_query->query['orderby'] ) && 'color' == $wp_query->query['orderby'] ) {

        $clauses['join'] .= <<<SQL
LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
SQL;

        $clauses['where'] .= " AND (taxonomy = 'color' OR taxonomy IS NULL)";
        $clauses['groupby'] = "object_id";
        $clauses['orderby']  = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) ";
        $clauses['orderby'] .= ( 'ASC' == strtoupper( $wp_query->get('order') ) ) ? 'ASC' : 'DESC';
    }

    return $clauses;
}
add_filter( 'posts_clauses', 'color_clauses_mike', 10, 2 );
share|improve this answer

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.