Title: Custom post query
Last modified: August 22, 2016

---

# Custom post query

 *  [lokust](https://wordpress.org/support/users/lokust/)
 * (@lokust)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/custom-post-query-2/)
 * Hi, I have a custom post type to show some products.
    I need to sort those products
   on the archive index page by name. However I need to exclude a word “XYZ” from
   the ordering:
 * Product names are as follows and in the order they should appear:
 * XYZ Product a
    XYZ Product b Product c
 * Is there a way to do omit “XYZ” from the name ordering in the post query?

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [lokust](https://wordpress.org/support/users/lokust/)
 * (@lokust)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/custom-post-query-2/#post-5432995)
 * In case it helps someone else:
 * This was the solution, in functions.php
 *     ```
       add_filter('posts_orderby_request', 'my_posts_orderby_request', 10, 2);
       function my_posts_orderby_request($orderby, $query) {
           if ($query->get('clean_xyz')) {
               return "REPLACE(LOWER(post_title), 'xyz ', '') ASC";
           }
           return $orderby;
       }
   
       add_action( 'pre_get_posts', 'my_pre_get_posts' );
       function my_pre_get_posts($query) {
           if ($query->is_post_type_archive('product') && $query->is_main_query() && $query->get('post_type') == 'product' && !$query->get('orderby')) {
               $query->set('clean_xyz', true);
           }
       }
       ```
   
 *  [LeRedac](https://wordpress.org/support/users/leredac/)
 * (@leredac)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/custom-post-query-2/#post-5433000)
 * maybe to help other perssones 😉
 *     ```
       $value= 'YOUR QUERY TAXONOMY';
       $replace = str_replace('XYZ' ,'',$value);
       echo $replace ;
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Custom post query’ is closed to new replies.

## Tags

 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [post query](https://wordpress.org/support/topic-tag/post-query/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 2 replies
 * 2 participants
 * Last reply from: [LeRedac](https://wordpress.org/support/users/leredac/)
 * Last activity: [11 years, 7 months ago](https://wordpress.org/support/topic/custom-post-query-2/#post-5433000)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
