WordPress.org

Ready to get started?Download WordPress

Forums

WP_Query sort by custom field first then all else (1 post)

  1. Scott
    Member
    Posted 2 years ago #

    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; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic