visually sort ordering “custom post types” for end users

Now with those custom post types and other bits of content that
don’t necessarily need to be organized chronologically by date, for
example: the client has a 100 T-Shirts as custom post type “SHIRT” and
he wants to set their appearance order.

What approach you suggest taking for letting the editor/admin of the
site SORT their order?

Related posts

Leave a Reply

6 comments

  1. you can filter about taxonomie

            // to filter by category
        function restrict_manage_posts() {
            global $typenow;
    
            if ( FB_CPT_POST_TYPE_1 == $typenow ) {
                $args = array('orderby' => 'count', 'hide_empty' => true);
                echo $this->get_taxonomy_html_select(FB_CPT_TAXONOMY_TYPE_1, $args);
            }
        }
    
        function get_taxonomy_html_select($taxonomy_name, $args) {
    
            $taxonomy = get_taxonomy($taxonomy_name);
            $terms = get_terms($taxonomy_name, $args);
            $label = __( 'Show all ' . $taxonomy->label, FB_CPT_TEXTDOMAIN );
            $html = array();
            $html[] = '<select style="min-width:155px;" id="' . $taxonomy_name . '" name="' . $taxonomy_name . '" class="postform">';
            $html[] = '<option value="0">' . $label . '</option>';
            if ( isset($_GET[$taxonomy_name]) )
                $this_term = $_GET[$taxonomy_name];
            else
                $this_term = '';
            foreach($terms as $term) {
                $default = ( $this_term == $term->term_id ? ' selected="selected"' : '' );
                $value = esc_attr($term->name);
                $value = $value . '&nbsp;&nbsp;(' . (int)$term->count . ')';
                $html[] = "t" . '<option value="' . $term->term_id . '"' . $default . '>' . $value . '</option>';
            }
            $html[] = '</select>' . "n";
            return implode( "n", $html );
        }
    
        function request($request) {
            global $pagenow;
    
            if ( is_admin() && 'edit.php' == $pagenow && isset( $request[FB_CPT_TAXONOMY_TYPE_1] ) && FB_CPT_POST_TYPE_1 == $request[FB_CPT_TAXONOMY_TYPE_1] ) {
                $request['taxonomy'] = FB_CPT_TAXONOMY_TYPE_1;
                $request['term'] = get_term($request[FB_CPT_TAXONOMY_TYPE_1], FB_CPT_TAXONOMY_TYPE_1)->name;
                unset($request['name']);
            }
    
            return $request;
        }
    

    use this hooks

                // to filter custom post type per custom taxonomy
            add_action( 'restrict_manage_posts', array( &$this, 'restrict_manage_posts') );
            add_action( 'request', array( &$this, 'request' ) );
    
  2. In order o have a truly arbitrary sorting order (completely unrelated to any post field), you’d need to create a “sort value” (or “weight”) field and enter a numeric value for each item in that field. You can then sort on that field.

  3. If all you want to do is set a position for each “Shirt”, then why not just user the Menu Order, then you can sort by menu_order in your WP_Query. You will probably have to make a meta box which sets the post’s menu_order property. Then in your WP_Query:

    $shirts = new WP_Query( 'post_type=shirt&orderby=menu_order&order=ASC' );

    This would work similar to how you order images in the Media Upload Gallery tab, for set Page’s Menu Order.

    Or am I misunderstanding something?

  4. What I prefer to do if I’m creating the post type in a plugin or in code is to also add this code, which allows you to set a default order for the front and back-end and also set the GET variable in admin so that the WordPress admin recognizes the change and puts the arrow up if you’re sorting by title, date, or anything else you are showing in the admin table.

    add_filter( 'pre_get_posts' , 'my_cpt_order' ); // Runs before the posts are fetched
    function my_cpt_order( $query ) {
        // Check query and alter only the query needed
        //echo '<PRE>'; print_r($query); echo '</PRE>'; 
        if ($query->query['post_type'] == 'cpt' && !isset($query->query['orderby'])) {
            $query->set( 'orderby' , 'title' );
            $query->set( 'order' , 'asc' );
            //get the arrow to show up over title in admin
            if (is_admin()) {
                $_GET['orderby'] = 'title';
                $_GET['order'] = 'asc';
            }
        } //if
    } //my_cpt_order
    

    Since menu_order is the default this is not needed if you’re sorting a custom post type by menu_order but if it is a post type where you don’t want the user to have to worry about sorting themselves and you want to use something like title, it’s very handy.