0

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.

0

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.org/reference/classes/wp_query/#order-orderby-parameters

-1

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

  • Yes. I had thought of this, but the problem I run into is if there is no number/priority in the meta-data box, then it will not be displayed. This could be a problem for clients that use this feature that don't know what they should be looking for. – Michael Aug 25 at 22:42
  • I'm not familiar with CMB2, can't you just set a default value (either 0, or a maximum, say 999)? Also, I would've thought even without a value, you use the meta_key just for orderby so posts without ranking should still be displayed - just at the end? – pax Aug 27 at 5:10

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.