Advertisement
vtxyzzy

Remove The from post titles for sorting

May 23rd, 2012
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.12 KB | None | 0 0
  1. // Put these filters in functions.php or leave them here and put all of this in your template
  2. function mam_posts_fields ($fields) {
  3.    global $mam_global_fields;
  4.    // Make sure there is a leading comma
  5.    if ($mam_global_fields) $fields .= (preg_match('/^(\s+)?,/',$mam_global_fields)) ? $mam_global_fields : ", $mam_global_fields";
  6.    return $fields;
  7. }
  8. function mam_posts_orderby ($orderby) {
  9.    global $mam_global_orderby;
  10.    if ($mam_global_orderby) $orderby = $mam_global_orderby;
  11.    return $orderby;
  12. }
  13. add_filter('posts_fields','mam_posts_fields');
  14. add_filter('posts_orderby','mam_posts_orderby');
  15.  
  16. // Sample showing how to set the filters and display the results
  17. $mam_global_fields = ", IF($wpdb->posts.post_title REGEXP('^the '),CONCAT(SUBSTR($wpdb->posts.post_title,5), ', ', SUBSTR($wpdb->posts.post_title,1,4)),$wpdb->posts.post_title) AS sort_title";
  18. $mam_global_orderby = " UPPER(sort_title) ASC";
  19. query_posts("posts_per_page=-1");
  20. $mam_global_fields = $mam_global_orderby = '';  // Turn off filters
  21. if (have_posts()) {
  22.    while (have_posts()) {
  23.       the_post();
  24.       echo "$post->sort_title<br />";
  25.    }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement