Title: Custom QP
Last modified: August 21, 2016

---

# Custom QP

 *  Resolved [ajc458](https://wordpress.org/support/users/ajc458/)
 * (@ajc458)
 * [12 years, 9 months ago](https://wordpress.org/support/topic/custom-qp/)
 * I have a custom query set up for a website that’s ordering the posts by the 2nd
   word of the post title (they are names, so it’s ordering by last names). My custom
   query is working perfectly in wordpress 3.7, but in 3.8 it does absolutely nothing.
   This is the code I wrote:
 *     ```
       function custom_get_posts($query) {
   
       	if (is_category()) {
   
       		global $wpdb;
       		global $post;
   
       		if(is_category(14)) {
   
       			$querystr = "
       			SELECT DISTINCT wposts.ID
       			FROM $wpdb->posts wposts
       				LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id
       				LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
       				LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
       			WHERE $wpdb->term_taxonomy.term_id IN ('14')
       				AND wposts.post_status='publish'
       			ORDER BY SUBSTR(LTRIM(wposts.post_title), LOCATE(' ', LTRIM(wposts.post_title))) ASC
       			";
   
       			$pageposts = $wpdb->get_results($querystr, OBJECT);
   
       			$pageposts_arr = array();
       			foreach ($pageposts as $pagepost) {
       				array_push($pageposts_arr, $pagepost->ID);
       			}
   
       			if($query->is_main_query()) {
       				$query->query_vars['post__in'] = $pageposts_arr;
       				$query->query_vars['orderby'] = 'post__in';
       			}
   
       		} elseif (is_category(24)) {
       			if($query->is_main_query()) {
       				$query->query_vars['orderby'] = 'modified';
       			}
       		}
   
       	}
   
       	return $query;
       }
       add_filter('pre_get_posts', 'custom_get_posts');
       ```
   

 The topic ‘Custom QP’ is closed to new replies.

 * 0 replies
 * 1 participant
 * Last reply from: [ajc458](https://wordpress.org/support/users/ajc458/)
 * Last activity: [12 years, 9 months ago](https://wordpress.org/support/topic/custom-qp/)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
