Title: WP_Query sort by custom field first then all else
Last modified: August 20, 2016

---

# WP_Query sort by custom field first then all else

 *  [Scott](https://wordpress.org/support/users/skotmxpx/)
 * (@skotmxpx)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/wp_query-sort-1/)
 * I have a custom post type of Products that I’m attempting to sort. Only some 
   of those products have a custom field key (via WPAlchemy) of “New”.
 * What I would like is to use WP_Query to display all my Products alphabetically
   but sort those with the custom field key of New at the top. And everything else
   that doesn’t not have the “New” key below.
 * In short I’d like our newer Products to float at the top until we remove the “
   New” custom field. And everything else below them.
 * I’m trying to avoid 2 queries, but if that’s the only way possible, so be it 
   I guess.
 * Here’s what I have so far, it only displays the New products for right now.
 *     ```
       <?php $loop = new WP_Query(
       	array(
       		'post_type' => 'products',
       		'posts_per_page' => 20,
       		'order' => 'ASC',
       		'meta_key' => '_new_banner',
       		)
       ); ?>
   
       <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
   
       <em>do stuff</em>
   
       <?php endwhile; ?>
       ```
   

The topic ‘WP_Query sort by custom field first then all else’ is closed to new replies.

## Tags

 * [meta_key](https://wordpress.org/support/topic-tag/meta_key/)
 * [meta_query](https://wordpress.org/support/topic-tag/meta_query/)
 * [meta_value)](https://wordpress.org/support/topic-tag/meta_value/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Everything else WordPress](https://wordpress.org/support/forum/miscellaneous/)
 * 0 replies
 * 1 participant
 * Last reply from: [Scott](https://wordpress.org/support/users/skotmxpx/)
 * Last activity: [14 years, 10 months ago](https://wordpress.org/support/topic/wp_query-sort-1/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
