Support

Account

Home Forums ACF PRO Orderby different meta_key than query

Solved

Orderby different meta_key than query

  • Hi there,

    I’m using https://www.advancedcustomfields.com/resources/query-posts-custom-fields/ to query for a custom post type meta_key. However, i’d like to sort (orderby) a different meta_key than the one that is queried.

    $locs = get_posts(array(
    'posts_per_page'	=> -1,
    'post_type'			=> 'locations',
    'meta_key'			=> 'monday_start_time',
    'orderby'			=> 'monday_start_time',
    'order'				=> 'ASC'
    ));

    This seems to work, but i’d like to query for meta_key Monday, and then sort by monday_start_time.

    Is this possible? Alternatively a solution would be to hide (not query?) locations which do not have a monday_start_time set.

    Hope someone can help me out. Thanks in advance!

    Best,
    Dave

  • Seemed to have fixed it by using this. Maybe someone knows if this is the correct way? Maybe there’s a cleaner way?

    $locs = get_posts(array(
    	'posts_per_page'	=> -1,
    	'post_type'			=> 'locations',
    	'orderby' 			=> 'monday_start_time',
        'order'				=> 'ASC',
    	'meta_query' => array(
            array(
                'key'     => 'monday_start_time',
                'value'   => '',
                'compare' => '!=',
            ),
        )
    ));
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Orderby different meta_key than query’ is closed to new replies.