EDIT 2: Rather than figuring out the real solution I’ve decided to use a workaround, and am simply setting the default value for book_in_series to ‘0’. Thanks to @eddiemoya for all his the time he spent looking at this with me!

EDIT: I had a different question with required posting the full code for this archive page / query, if you’d like to see that it’s here.

This query is working great, but doesn’t include any posts which don’t have the meta_key ‘book_in_series’ (which determines the sort value). What I’d like to have happen is for the books to be sorted by this value if it exists, otherwise come back unordered. Is that possible?

$args = array( 
       'post_type' => 'books', 
       'posts_per_page' => -1,
       'nopaging' => true,
       'surpress_filters' => true,
       'orderby' => 'meta_value',
       'meta_key' => 'book_in_series',
       'order' => 'ASC',
       'post_parent' => $current_series_id,
 );

 $books = new WP_Query( $args);

It seems like this answer from Rarst comes close, but I’m not sure how to adapt it for my circumstances. Custom query with query_posts doesn’t show post without certain meta_key

Thanks in advance for your help!

4 s
4

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:

Leave a Reply

Your email address will not be published. Required fields are marked *