9

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?

3
  • Can you give some examples of what types of things he'd want to sort them by? Some answers may be better for some things than for others. Aug 12, 2010 at 1:21
  • Mike: maybe t-shirts where he'd want certain to be first, or generally any post that the user inserted not in the correct order.
    – Amit
    Aug 12, 2010 at 14:19
  • For those still searching that run across this post, there are some nice WP plugins that solve this problem. I've just implemented this one: wordpress.org/extend/plugins/post-types-order
    – user9544
    Oct 18, 2011 at 18:40

6 Answers 6

1

I might suggest these:

http://drewgourley.com/order-up-custom-ordering-for-wordpress/

1
  • download link not working on given url
    – Shiv Singh
    Feb 18, 2018 at 9:38
3

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' ) );
4
  • I tried to implement it but it doesn't work for me.. are there any previous steps I should take? declaring some taxonomy? thanks!
    – Amit
    Aug 13, 2010 at 17:27
  • its an example on my class for custom post type; you must change array(&this etc.) for not use in a class and the constant FB_CPT_TAXONOMY_TYPE_1 is my first custom taxonomy and so on adn the constant FB_CPT_POST_TYPE_1 is my first custom post type
    – bueltge
    Aug 13, 2010 at 18:57
  • please see this answer (wordpress.stackexchange.com/questions/45/…), is similar
    – bueltge
    Aug 13, 2010 at 19:14
  • I understand.. I probably didn't explain myself very good - what I meant is letting the user SET the sort order and this new order should be reflected on the front-end. whatI'm actually looking for is something like the new menus gui but for posts/custom posts type.
    – Amit
    Aug 13, 2010 at 21:20
0

I would look at the menu generator admin screens. Since a menu is just a custom post type, i'm sure some of that stuff can be used as inspiration..

1
  • that's not a bad idea at all, but do menus hold infinite numbers of items (over 100)? wouldn't it be a bit of a bloat to have an entry item for each post?
    – Amit
    Aug 11, 2010 at 19:42
0

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.

0

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?

2
  • you got it right, but think of the user.. he needs to go manually to each posts and set a number in the meta box.. not very friendly..
    – Amit
    Aug 13, 2010 at 21:21
  • Incorporating it into Quick Edit would make that a lot easier, but yes it could be time consuming.
    – Joe Hoyle
    Aug 15, 2010 at 12:50
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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