• Resolved johnnyriss

    (@johnnyriss)


    Greetings,

    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: https://cl.ly/image/0P0W1u1M2z25

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

    Much thx!

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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.

    Moderator keesiemeijer

    (@keesiemeijer)

    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:

    <?php
    $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' );
    ?>

    https://codex.www.ads-software.com/Plugin_API/Filter_Reference/posts_clauses

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

    Thread Starter johnnyriss

    (@johnnyriss)

    @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.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Order and Orderby not functioning as specified’ is closed to new replies.