Custom Taxonomy order by Custom Field

Ok so after posting the question an option to view another similar question appeared. So here is the answer if somebody is trying to do the same thing.

//Using WordPress Pre-Get filter to order the custom taxonomy by custom field
function customize_customtaxonomy_archive_display ( $query ) {
    if (($query->is_main_query()) && (is_tax('custom-taxonomy')))

    $query->set( 'post_type', 'your-post-type' );                 
    $query->set( 'posts_per_page', '-1' );
    $query->set( 'meta_key', 'your-custom-field' );           
    $query->set( 'orderby', 'meta_value_num' );
    $query->set( 'order', 'ASC' );
}

 add_action( 'pre_get_posts', 'customize_customtaxonomy_archive_display' );

Add this to your functions.php file, or create a plugin for it. Replace “custom-taxonomy”, “your-post-type” and “your-custom-field” with your details and you should get the results you are after.

Dan

Leave a Comment