Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom post types: Order by title; 10 ordered wrong, after 2 WordPress

  • SOLVED

I have a custom post type, called reviews and I try to order the posts by title. I gave to the titles numbers 1-10.

The problem is that WordPress sees those as alphanumeric characters and 10 follows 1.
How do I make 10 to be 10?

I was thinking to order them by custom fields, which I think should work, but this solution would be the easiest for the client.

<?php
$loop = new WP_Query( array( 'post_type' => 'reviews', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => 10 ) );
?>


Answers (4)

2010-09-18

Utkarsh Kukreti answers:

Add this to your functions.php

function orderby_post_title_int( $orderby )
{ return '(wp_posts.post_title+0) ASC'; }


Add this just before you run the query

add_filter('posts_orderby', 'orderby_post_title_int' );

Add this after the query is executed

remove_filter('posts_orderby', 'orderby_post_title_int' );


Lucian Florian comments:

Utkarsh, you seem to be very close to solve my problem, but I get a error:
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'orderby_post_title_int' was given in /home/content/19/6467119/html/HOD/wp-includes/plugin.php on line 220

This is the whole code I have in my query now:
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=1'.'&paged='.$paged);
add_filter('posts_orderby', 'orderby_post_title_int' );
$loop = new WP_Query( array( 'post_type' => 'reviews', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => 10 ) );
remove_filter('posts_orderby', 'orderby_post_title_int' );
?>


Utkarsh Kukreti comments:

Lucian, you have you added the function (1st code) in your functions.php?


Lucian Florian comments:

Utkarsh, it works. You are a genius.

Now I need to sort the back-end too using your method. This is the code I have that does the sorting. Let me know if you can help with this one too, if not, I will select you as a winner anyways!

//order back-end posts 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 == 'reviews') {

// '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');


Utkarsh Kukreti comments:

function set_custom_post_types_admin_order($wp_query) {
if (is_admin()) {
add_filter('posts_orderby', 'orderby_post_title_int' );
}
}

add_filter('pre_get_posts', 'set_custom_post_types_admin_order');


should work.


Lucian Florian comments:

Thank you ! I really appreciate your help.

2010-09-18

Monster Coder answers:

isn't it should be like
$loop = new WP_Query( array( 'post_type' => 'reviews', 'orderby' => 'post_title', 'order' => 'ASC', 'posts_per_page' => 10 ) );

that mean, you wrote 'title' but it should be 'post_title'


Lucian Florian comments:

Thanks for you quick input. Now, the number 10 is after 7!


Monster Coder comments:

hello,
the post_title field is designed as string! it 10 will follow 1! you won't get 1.2.3.....9.10 sorting using it! however, you can convert type on the fly if that is highly required! you will need to write custom query (SQL) for that!

you can also use manual blog re-ordering plugin!

I built this plugin quite long ago, and I don't know whether it works with custom post type or not!
[[LINK href="http://hungrycoder.xenexbd.com/scripts/wordpress-blogpost-reorder.html"]]http://hungrycoder.xenexbd.com/scripts/wordpress-blogpost-reorder.html[[/LINK]]

2010-09-18

Rashad Aliyev answers:

An example of order by title.

query_posts('caller_get_posts=1&author=1&post_type=page&post_status=publish&orderby=title&order=ASC');


use this standard on your query.


Lucian Florian comments:

Rashad, I do not see your whole code. Please use the code box.

2010-09-18

Denzel Chia answers:


query_posts('post_type=reviews&orderby=title&showposts=10&order=ASC');
while (have_posts()) : the_post();
// the content of the post
the_content();
endwhile;


Hope it helps!