One of the long-awaited new features of WordPress 3.0 is custom post types.
By default, custom posts with the post capability_type (as opposed to page, attachment, revision, or nav-menu-item) are listed newest to oldest in the WordPress backend. This makes sense if these are blog posts, but if they are some other content type (such as quotes, movies, books or contacts) it is often desirable to have them listed alphabetically.
To achieve this, add the following code to your functions.php file, replacing POST_TYPE (line 7) with the name of the post type that you want to have listed alphabetically.
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_filter('pre_get_posts', 'set_custom_post_types_admin_order');
Question: does this mean it is now possible in WordPress to have a “custom post type” of “link”, like I currently have on my website? (In other words, clicking on the title goes to another website, rather than my own, and is styled differently.) If so, and I have a free couple of days, I might switch to WordPress as I keep on having to hack Chyrp about to fix it and development just halted on future releases!
Hi Matt,
Using Custom Fields (http://codex.wordpress.org/Custom_Fields) and a little template file tweaking it should be possible to do what you want. Here’s how I’d go about it:
1. On Posts that I wanted to link to an external URL, I’d add a Custom Field, calling it something like ‘external-link’ and giving it the value of the external URL.
2. Edit the WordPress Loop so that it checks to see if the ‘external-link’ custom field is set. If it isn’t set, display the post title with the internal link as usual. If it is set, link to that external link instead. The Loop appears in most WordPress template files (index.php, single.php, archive.php etc.), so you’d need to check through them to make sure you changed them all. I’m assuming you’ve come across the WordPress Loop before; if not, here’s the documentation page http://codex.wordpress.org/The_Loop.
As an alternative to using a Custom Field, you could add a meta box (like the author box) to the Post admin interface using add_meta_box (http://codex.wordpress.org/Function_Reference/add_meta_box). This would look a bit smarter I reckon, although functionally it does the same thing.
If you try this but get stuck on tweaking the Loop output in the template files let me know and I should be able to point you in the right direction.
Mark,
Thanks for this… Any idea how to get your custom post type to be editable order (like Pages are)?
That would come in handy. I am happy to have custom post types, but there isn’t a lot of documentation out there yet.
Thanks
Hi creativereason,
I haven’t tried doing this myself, but the following post might start you in the right direction: http://groups.google.com/group/wp-hackers/msg/41111adb76b80a83
Mark
I’ve got a tutorial on this, I use movies as an example – but the functions and concepts are modular enough I’m sure you could modify them to suit,
http://www.eggplantstudios.ca/blog/wordpress-order-custom-post-type-by-custom-field-in-the-admin-area
thanks so much for sharing this shawn! it’s exactly what i was looking for.
Maybe you should just try the Post Types Order plugin? something like what PostMash does but for all post custom types including posts and pages
In fact your could just set ‘hierarchical’ to “true” in the register_post_type declaration.
Thanks, Mark! This is exactly what I was looking for. It seems like such a common need for custom post types I’m surprised there’s no option to set this during the registration process.
Thanks for this, it was just what I was looking for.
I’d like to add some code to solve a problem I had:
Add
!isset($_GET['orderby'])
before the set function to prevent the filter from overriding the sort order when you explicitly ask (eg. clicking on the post author or post date).My function then becomes
function app_list_admin_order($query) {
if (is_admin()) {
$post_type = $query->query['post_type'];
if ($post_type = 'custom_post_type') {
if (!isset($_GET['orderby'])) {
$query->set('orderby', 'title');
$query->set('order', 'asc');
}
}
}
}
Thank you, that was helpful.
Thanks man!
It doesn’t work for me, and i need it ASAP
my post type name : clavebloc (‘desc-project’,’date-project’)
and i want to order them by ‘date-project’ column
i replaced it but nothing happend
I Need Help please !!
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 == ‘clavebloc’) {
// ‘orderby’ value can be any column name
$wp_query->set(‘orderby’, ‘date-projet’);
// ‘order’ value can be ASC or DESC
$wp_query->set(‘order’, ‘ASC’);
}
}
}
add_filter(‘pre_get_posts’, ‘set_custom_post_types_admin_order’);
Thanks, for the solution. Though, it wasn’t exactly what I wanted to know, but clearly give me an idea on how to fix my issue.
Thanks again! 🙂
Thanks! Exactly what I needed ; )
Hello, Neat post. There is an issue along with your site in internet explorer, may test
this? IE still is the market chief and a good element of other folks will pass over your fantastic writing due to this problem.
is there a way to do this for a post title with being a name and sorting by the last name? Ie John Adams would be the post title and sort by his LASTName?
Not if you are sorting by title, but I think you can achieve the same effect by either:
1. Setting the author name to be Last Name, First Name and then sorting results by author, or
2. Adding a meta_value containing the last name and sorting by that meta_value
See https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters for reference.
Works – brilliant! Thanks for the code snippet.