admin管理员组

文章数量:1415100

I am trying to create a custom way to order my new custom post type.

Here is what I am envisioning:

  • There would be a custom way to order posts in an entirely unique order for display (Number, automatic value, etc.) This would be so I could avoid needing to manually change a ton of publication dates on posts to get the right order.
  • If there was no number/automatic value/etc. entered then it would display based on when it was published at the end of the list.

I am using CMB2 for the meta-data fields on my custom post type.

Does anyone have any suggestions or ideas? I realize that this is a complicated problem, but I want to be able to order posts in a custom order that is not by author, date published, or title.

I am trying to create a custom way to order my new custom post type.

Here is what I am envisioning:

  • There would be a custom way to order posts in an entirely unique order for display (Number, automatic value, etc.) This would be so I could avoid needing to manually change a ton of publication dates on posts to get the right order.
  • If there was no number/automatic value/etc. entered then it would display based on when it was published at the end of the list.

I am using CMB2 for the meta-data fields on my custom post type.

Does anyone have any suggestions or ideas? I realize that this is a complicated problem, but I want to be able to order posts in a custom order that is not by author, date published, or title.

Share Improve this question asked Aug 25, 2019 at 16:28 MichaelMichael 881 silver badge7 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

With register_post_type(), you can specify the 'page-attributes' => true property, and you'll get the "menu order" metabox which you see on pages. With this you can custom order your items, where the default is always 0, which means they will always show, even if you don't set a value.

You don't actually have to set the page-attributes attribute to use the the menu_order post property/column. You can set that value programmatically. To query with the custom ordering in place, you can use the menu_order property in your WP_Query args: https://developer.wordpress/reference/classes/wp_query/#order-orderby-parameters

You could just add a 'priority' or 'order' custom field (number) to your post type and order based on that.

See Custom query with orderby meta_value of custom field, wp_query / #order-orderby-parameters

本文标签: Order Posts in Custom Order