Love, love love the ACF! Use it heaps, but am having difficulty figuring out how to do a particular query:
For an example if you have a website of doctors, and you wish to get a list of all doctors based on a custom template, and order by the doctor’ last name, then sub-sorting by first name.
$allDocs = get_posts(array(
‘post_type’ => ‘page’,
‘numberposts’ => -1,
‘meta_key’ => ‘_wp_page_template’,
‘meta_value’ => ‘bio-detail.php’,
‘orderby’ => ‘post_title’,
‘order’ => ‘asc’
The query above works fine without any issues, the stumbling block I encounter is when I wish to order by some of the custom fields like ‘last_name’ and ‘first_name’ instead of the post’s title?
If you want to order by a custom field then you need to add a meta_query and use clauses, for more information take a look at https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/