[resolved] Order and Orderby not functioning as specified (4 posts)

  1. johnnyriss
    Posted 2 years ago #


    I have a site in with a custom post type representing patents. Each patent number is the Post Title.
    Some start with letters, most start with numbers.
    The higher the patent number, the more recent it is.
    So, I have the query set like so:

    $args = array (
          	'post_type' => 'patent',
          	'order' => 'DESC',
          	'orderby' => 'title',
          	'posts_per_page' => -1,
          $patent_query = new WP_Query( $args );

    The problem is that the order is putting the letters at the top. I want the numbers all to be at the top, then the letters.
    On the codex page it says that's how it should be showing.

    Order & Orderby Parameters
    Sort retrieved posts.
    order (string) - Designates the ascending or descending order of the 'orderby' parameter. Defaults to 'DESC'.
    'ASC' - ascending order from lowest to highest values (1, 2, 3; a, b, c).
    'DESC' - descending order from highest to lowest values (3, 2, 1; c, b, a).

    But the results are not doing that.
    It's doing (c, b, a, 3, 2, 1) see here: http://cl.ly/image/0P0W1u1M2z25

    Any thoughts on how to make the results show as they're supposed to?

    Much thx!

  2. catacaustic
    very awesome
    Posted 2 years ago #

    If you look at what the codex page says, it says:

    (1, 2, 3; a, b, c)

    Notice the semi-colan in there? That's the give-away to your issue. That means that the two sides are not connected. So it's "3,2,1 OR c,b,a", not "3,2,1,c,b,a". I have to admit that the way they've put it on that page is going to lead to a lot of confusion.

    The standard way of sorting using the MySQL functions like that are most symbols, then 0-9 and then a-z when accending, and the opposite when decending, so your results are exactly what is expected for the standard sorting process.

  3. keesiemeijer
    Posted 2 years ago #

    Try it with adding the sorting you want with the posts_clauses filter.
    In your theme's functions.php

    function custom_column_sort( $pieces ) {
    	global $wpdb;
    	$pieces['fields'] .= ", $wpdb->posts.post_title AS sort";
    	$pieces['orderby'] = " ($wpdb->posts.post_title +0) DESC, sort DESC";
    	return $pieces;

    Query like this:

    $args = array (
    	'post_type' => 'patent',
    	'posts_per_page' => -1,
    // sets the order to numbers first descending, titles second descending
    add_filter( 'posts_clauses', 'custom_column_sort' );
    $patent_query = new WP_Query( $args );
    remove_filter( 'posts_clauses', 'custom_column_sort' );


    consider creating a child theme instead of editing your theme directly - if you upgrade the theme all your modifications will be lost.

  4. johnnyriss
    Posted 2 years ago #

    @catacaustic, I hear ya. I see that pesky semi-colon now. Thx for explaining.

    @keesiemeijer, Awesome! Works like a charm! And, yep. I'm using a child theme.


Topic Closed

This topic has been closed to new replies.

About this Topic