Sorting Posts by custom field

To expand on @pieter-goosen’s comment, you do indeed want to use pre_get_posts. In your example, by using WP_Query, you’re overwriting the entire query and just resetting most parts of it to default. In fact, you’re probably not seeing a specific category of posts at all. You should be seeing all posts since that’s the default of the WP_Query class.

So instead, use pre_get_posts which modifies an existing query rather than creating a brand new one. Here’s a [untested] snippet that should work in your functions.php file:

add_action( 'pre_get_posts', 'wpse183601_filter_category_query' );
function wpse183601_filter_category_query( $query ) {
    // only modify front-end category archive pages
    if( is_category() && !is_admin() && $query->is_main_query() ) {
        $query->set( 'posts_per_page','20' );
        $query->set( 'orderby','meta_value' );
        $query->set( 'meta_key','artwork_title' );
        $query->set( 'order','ASC' );
    }
}

Leave a Comment