• Resolved pixelnate

    (@pixelnate)


    I have a custom post type of ‘inductees’ and I need to list out these inductees in a ‘last name, first name’ order. The problem is that the titles for each of the inductees is ‘first name last name’. Well, I have a filter working on the query, but the inductee order remains based on the first word (first name) of the inductees. How can I sort the data based on the filtered post titles?

    Here is the code I am working from:

    <?php
    global $wp_query;
    $inductees = query_posts( array(
      'post_type'       => 'inductee',
      'posts_per_page'  => -1,
      'orderby'         => 'title',
      'order'           => 'ASC'
      )
    );
    
    function sort_inductee_names( $golfer ) {
      $golfer_array = explode( ' ', $golfer );
      $golfer_last_name = array_pop( $golfer_array );
      $golfer = $golfer_last_name . ', ' . ( join( ' ', $golfer_array ) );
      return $golfer;
    }
    add_filter( the_title, sort_inductee_names, 1 );
    ?>

Viewing 8 replies - 1 through 8 (of 8 total)
  • I think using the filters below will work (Not fully TESTED):

    function add_title_name_fields ($fields) {
       $fields .= ", SELECT substring_index(post_title,' ',1) as fname, reverse(substring_index(reverse(post_title),' ',1)) as lname ";
       return $fields;
    }
    add_filter('posts_fields','add_title_name_fields');
    
    function sort_on_names($orderby) {
       $orderby = " UPPER(lname), UPPER(fname) ";
       return $orderby;
    }
    add_filter('posts_orderby','sort_on_names');

    However, you may find that names are not as easy as this. For example, what about the guy named Brad de la Cruz? Or J. C. Knight?

    Thread Starter pixelnate

    (@pixelnate)

    Sam, good to have you on the other end of a question again. Nice looking solution, too, but I’ll have to wait until Monday to test it when I get in the office. Sometimes it takes a few days to get an answer in here and I wanted to cast the net and see what came up before heading into the week.

    You are right about the strange/non-standard names. I have a couple of juniors and seniors in there, and maybe one of two multi-word last names. I could turn that first filter into an if-then-else statement couldn’t I?

    Been following Emily’s blog, too. It’s good to hear that she’s making progress. I cannot imagine what y’all must be going through.

    Would it be out of the question to add a Custom Field with last, first and then sort on that?

    Thread Starter pixelnate

    (@pixelnate)

    That would be the quickest way to solve the problem, but it seems so amateur to tell my client that they would need to enter the names of the golfers twice. I’ll bring that up to the sales rep, but it still feels a bit dirty.

    OK, what about having the client enter last, first as the post title and then reverse it when you display? It is a lot easier and more consistent to reverse a comma separated name than to parse a ‘first last’ name.

    One other thought: If you try to sort after extracting from the database, pagination could be messy.

    Thread Starter pixelnate

    (@pixelnate)

    That’s a good idea. It would be a lot easier to reformat the content. Fortunately I don’t need to paginate the results and there are only about 60 names to reverse. I think that’s the way I’ll go. Thanks. I’ll mark this as resolved and open a new thread later if I run into problems. Thanks, again.

    Just FYI, here is a code snippet that sorts the post title as if it were first last, with miminal checking for suffixes:

    function reverse_name($n) {
          // $n is a name in first last order.  Reverse to last, first
          $parts = explode(' ',strtoupper($n));
          $last = array_pop($parts);
          $last = preg_replace('#[^A-Z]#','',$last);  // Get rid of punctuation
          if (in_array($last,array('JR','SR','III'))) {
             $suffix = $last;
             $last = array_pop($parts);
             array_push($parts,$suffix);
          }
          $rev = $last . ', ' . implode(' ',$parts);
          return $rev;
       }
       function title_sort($a,$b) {
          $ta = reverse_name($a->post_title);
          $tb = reverse_name($b->post_title);
          $r = ($ta == $tb) ? 0 : ($ta > $tb) ? 1 : -1;
          return $r;
       }
       query_posts("posts_per_page=-1&caller_get_posts=1");
       if ( have_posts() ) {
          usort($wp_query->posts,'title_sort');
          while ( have_posts() ) {
             the_post();
             echo '<br />';
             the_title();
          }
       }
    ?>

    I am trying to do the same, reversing the word order in a title. Did you resolve this issue, because I have tried the above code snippet in several different ways, but ti does not seem to work. many thanks.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How can I re-sort results of a query after filtering’ is closed to new replies.