0

I have a custom post type that's used to display properties on a client website. I am sorting the posts by the title, but want to ignore any numbers included in that title.

How would I sort posts by the first letter [A-Z], not [0-9, A-Z]?

This is my current loop:

<section id="grid" class="project clearfix">
    <div class="wrapper">
        <ul id="project" class="clearfix">
        <?php
        // select our project post types
        $wptricckspost = array( 
            'post_type' => 'project',
            'orderby'   => 'title', 
            'order'     => 'ASC' 
        );
        // Create the new loop
        $loop = new WP_Query( $wptricckspost );
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <li class="item">
                <header>
                    <h2><?php the_title(); ?></h2>
                </header>
                <article>
                    <?php the_content();
                    // Other HTML stuff below...
1
  • Where would numbers go in that sort? Might make sense to simply exclude such from the set...
    – Rarst
    Nov 10 '14 at 14:46
2

This is a bit messy to do in MySQL, given the lack of a preg_replace - the following uses a nested REPLACE to strip all numbers from the title:

function wpse167989_posts_orderby( $orderby, $query ) {
    global $wpdb;
    // Strip all numbers from title, and trim any leading spaces.
    return $wpdb->prepare(
        'LTRIM(' . str_repeat( 'REPLACE(', 10 ) . $wpdb->posts . '.post_title'
        . str_repeat( ', %s, %s)', 10 ) . ') ' . $query->get( 'order' )
        , 0, '', 1, '', 2, '', 3, '', 4, '', 5, '', 6, '', 7, '', 8, '', 9, '' );
}

Then

add_filter( 'posts_orderby', 'wpse167989_posts_orderby', 10, 2 );
$loop = new WP_Query( $args );
remove_filter( 'posts_orderby', 'wpse167989_posts_orderby', 10 );

EDIT per comments: to remove hyphens in addition, the filter becomes

function wpse167989_posts_orderby( $orderby, $query ) {
    global $wpdb;
    // Strip all numbers & hyphens from title, and trim any leading spaces.
    return $wpdb->prepare(
        'LTRIM(' . str_repeat( 'REPLACE(', 11 ) . $wpdb->posts . '.post_title'
        . str_repeat( ', %s, %s)', 11 ) . ') ' . $query->get( 'order' )
        , 0, '', 1, '', 2, '', 3, '', 4, '', 5, '', 6, '', 7, '', 8, '', 9, '', '-', '' );
}
4
  • Thanks Bonger, I'll test this out tonight and let you know how I get on. Nov 11 '14 at 14:54
  • Thank you @bonger, this works perfectly. would there be a way of adding a hyphen to this too. So for example if the number looks like this: 8-10 Title Here. Nov 12 '14 at 11:16
  • Yes, just change the two 10 arguments to the str_repeat's to 11 and then append , '-', '' to the prepare parameters - I'll update the answer...
    – bonger
    Nov 12 '14 at 14:19
  • That makes sense, I tried to add '-', '' yesterday but didn't realise that the 10 represents the amount of arguments. Thanks once again. Nov 12 '14 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.