Wordpress - Way to include posts both with & without certain meta_key in args for wp_query?

To be clear, you mean to say that any items that don't have book_in_series key will just be at the bottom and unordered, but the ones that do have it will be at the top and ordered ASC by that key. Right?

meta_query is used to specify what items you want to be returned, orderby is used to specify what to sort those items by. However, meta_key (which is deprecated as a means to specify which posts to get) is needed if you want to use orderby.

You need to add to that query, meta_query that specifies the posts you want. I suggest removing the meta_key while testing it, so that you know meta_key is not reverting to its old behavior. Once you know you have the posts you want, put the meta_key and orderby back in, and it should sort those posts accordingly.

http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

http://scribu.net/wordpress/advanced-metadata-queries.html

can I orderby one custom field and meta_query another in the same query

EDIT: A rough attempt at what your query should look like, read comments of explanation.

 $meta_query = array(
    'relation' => 'OR',
    array(
        'key' => 'book_in_series',
        'compare' => 'IN'
    ),
    array(
        'key' => 'book_in_series',
        'compare' => 'NOT IN'
    )
);

$args = array(
    'post_type'        => 'books',
    'posts_per_page'   => -1,
    'nopaging'         => true,
    'subpress_filters' => true,
    'meta_query'       => $meta_query,
    //'orderby'        => 'meta_value',
    //'meta_key'       => 'book_in_series',
    //'order'          => 'ASC',
    'post_parent'      => $current_series_id
);
 $query = new WP_Query( $args );

I've commented out the items related to sorting - if with this query, you get the posts you want, then try to remove the comments and see if it sorts without overriding the meta_query

Tags:

Wp Query