• I have a custom post type created that I need to have sorted alphabetically by title. I know how to do this myself in my own custom wp_query loop, but how can I get WordPress to sort everything automatically when a single custom post type record is displayed using the single.php file?

    The problem is that, when the previous/next posts pagination is displayed (using previous_post_link, etc) the posts are always in either chronological or ID order (can’t tell which).

    I’m sure there must be something that I can add to my functions.php file.

    Cheers

Viewing 9 replies - 1 through 9 (of 9 total)
  • You will need the posts_* filters, at least the posts_orderby one.

    Thread Starter Peter Hardy-vanDoorn

    (@petervandoorn)

    Hi s_ha_dum

    Thanks for the input, but I honestly don’t know what to do with that! How would I wrap that in a function to override the default sort order?

    Thanks

    There are examples on the Codex page.

    // Create a new filtering function that will add our where clause to the query
    function filter_where( $where = '' ) {
    	// posts  30 to 60 days old
    	$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
    	return $where;
    }
    
    add_filter( 'posts_where', 'filter_where' );

    You will need ‘posts_orderby’ not ‘posts_where’ but structurally it is the same.

    Thread Starter Peter Hardy-vanDoorn

    (@petervandoorn)

    Thanks again, and I followed your example, but am now getting 404s on EVERYTHING (pages, posts, the lot!).

    function filter_orderby( $where = '' ) {
    	$where .= " AND posts_orderby >= 'title'";
    	return $where;
    }
    add_filter( 'posts_orderby', 'filter_orderby' );

    Also (if it worked) I’m guessing that would change everything, but I want it to just affect a post type of ‘exhibitors’ – is there a way to narrow it down, as I do want normal posts to sort by the normal order?

    Cheers

    Ok. With those filters you are writing SQL. So, a couple of things…

    1. ‘posts_orderby’ is not valid SQL. You need ‘ORDER BY’
    2. ‘>=’ is an array assignment operator and is not valid SQL. You need just plain ‘=’
    3. ‘title’ isn’t a valid database column name. You want ‘post_title’.

    Yes, that would be global. Switch it using is_post_type_archive(). Put something like the following as the first line inside that function:

    if (!is_post_type_archive('exhibitors')) return $where;

    That should leave everything but the exhibitors post type unchanged. I haven’t tested it though.

    Thread Starter Peter Hardy-vanDoorn

    (@petervandoorn)

    Sigh! Still no luck – although no 404 errors now!

    Changed it as you suggested:

    function filter_orderby( $where = '' ) {
    	if (is_post_type_archive('exhibitors')):
    	$where .= " AND ORDER BY = 'post_title'";
    	endif;
    	return $where;
    }
    add_filter( 'posts_orderby', 'filter_orderby' );

    By the way, are you sure about using is_post_type_archive() – since I want it to sort on single pages, wouldn’t is_singular() be more appropriate? (not that changing it made any difference!)

    Thanks again

    You didn’t quite use the code I posted. Take another look.

    Oops. I guess I missed that you wanted this to work on single post type pages. is_single() won’t work for you. It doesn’t care about post type at all and will return true for any single post. You will need to check $wp_query['post_type'] for your post type name.

    Thread Starter Peter Hardy-vanDoorn

    (@petervandoorn)

    No, you’re right – I tidied it up! Anyway, it makes no difference. Neither does changing is_post_type_archive to is_singular.

    I’m very frustrated by all this now. I don’t suppose there’s any way you might consider actually giving me the correct code, rather than nudging me in the right direction with clues (which, by the way, I do appreciate as it’s the best way to learn about something as it makes one have to investigate and read up on the subject!)

    Cheers

    Neither does changing is_post_type_archive to is_singular.

    is_singular() won’t work for the same reason that is_single() won’t work.

    Try:

    function filter_orderby( $where = '' ) {
            global $wp_query;
    	if (!empty($wp-query['post_type'] && 'exhibitors' == $wp_query['post_type']) { // I hate that alternate syntax :)
    	       $where .= " ORDER BY = post_title";
    	}
    	return $where;
    }

    I noticed a couple of things. I can’t think of a reason why “ORDER BY” would ever be preceded by “AND”. I am not even sure if it is valid SQL. [edit]I tried it. Doesn’t work.[/edit] You don’t need or want the quote around “post_title” because you are referencing a database column name not a value. You can use backticks but single or double quotes will give you the wrong result (but not an error).

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Custom sort order in automatic wp_query’ is closed to new replies.