13

In a custom post type, is there a way to include the "order" field that is available for pages?

Is there any built-in logic that prevents duplicate values in the "order" field?

The idea is to be able to sort a custom post type by a user-specified order, then alphabetically by a string-based custom field.

24

When declaring your custom post type using the register_post_type function, you have to add 'page-attributes' to the support field, like in the following example:

register_post_type('myposttype', array(
    'supports' => array('title', 'editor', 'page-attributes'),
    'hierarchical' => false
));

You'll need to add any other supported meta boxes as well to the 'supports' field, see http://codex.wordpress.org/Function_Reference/register_post_type for more information about the register_post_type fields.

Also as far as I know there isn't any built in way to prevent two of the same order, this is because you can create sub-ordering based on heirarchy (so one group of children pages can have a different ordering than another)

  • using page-attributes presents both the order field and the parent field. How do I get rid of the parent field? – Force Flow Sep 19 '12 at 19:21
  • Nevermind. Setting hierarchical to false gets rid of it. – Force Flow Sep 19 '12 at 22:03
  • My bad, didn't realize you didn't also want the page parents. I'll update my answer to exclude it. – Dave Hunt Sep 20 '12 at 14:02
  • Hi. I have this code but I see nothing: ``` // Service Custom Post Type add_action( 'init', 'create_post_type' ); function create_post_type() { $supports_args = array( 'title', 'editor', 'page-attributes' ); register_post_type( 'service', array( 'labels' => array( 'name' => __( 'Services' ), 'singular_name' => __( 'Service' ), 'supports' => $supports_args, 'hierarchical' => false ), 'public' => true, 'has_archive' => true, //'menu_position' => 2 ) ); } ``` – Sylar Jul 20 '17 at 9:41
  • @Sylar I realize this is very late, but it looks to me like your 'supports' => $supports_args, 'hierarchical' => false is in the wrong part of the array. You have it nested under the labels array in your example, it should be in the base array, at the same level as 'public' => true – Dave Hunt Mar 14 '18 at 17:43
4

In addition to @Dave-Hunt's response, you can also add a filter, such as the following, to define a custom order - in this case, alphabetical by title. (Code thanks to Mark Leong's blog post.) Remove the is_admin() check, if you want you custom order_by on the front-end as well.

function set_custom_post_types_admin_order($wp_query) {
  if (is_admin()) {

    // Get the post type from the query
    $post_type = $wp_query->query['post_type'];

    if ( $post_type == 'POST_TYPE') {

      // 'orderby' value can be any column name
      $wp_query->set('orderby', 'title');

      // 'order' value can be ASC or DESC
      $wp_query->set('order', 'ASC');
    }
  }
}
add_action('pre_get_posts', 'set_custom_post_types_admin_order');

Update

For pre-save validation, see this answer: https://wordpress.stackexchange.com/a/40095/4645 where your options are discussed. Basically, it comes down to custom jQuery, as WordPress doesn't have any pre-save hooks.

Also (duplicating my previous comment here for future reference), here's how to expose the 'menu order' field in the admin, so it's user-editable, as it is for pages: Adding 'menu order' column to custom post type admin screen

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.