Search code examples
phpwordpressalphabetical

Cannot order destinations alphabetically


I have unordered destinations, which i print in the following way:

$destinations = get_posts( array(
                            'post_type' => 'destination_showcase',
                            'posts_per_page' => -1,
                            'post_status' => 'publish',
                            'meta_query' => array(
                                array(
                                    'key' => 'destination_state',
                                    'value' => ':"'.$state_id . '";' , // looking for serialized value in quotes
                                    'compare' => 'LIKE'
                                )
                            ),
                        ) );

I want to print the destinations alphabetically. When i var_dump-ed the #destinations array i got all of the destinations and their parameters. I want to get their titles i.e "post_title" and print it alphaberically. I've tried this, but doesn't work:

'orderby'=> $destinations->post_title, 'order' => 'ASC',

Any ideas how the task can be done ?


Solution

  • just try this

    $destinations = get_posts( array(
        'post_type' => 'destination_showcase',
        'posts_per_page' => -1,
        'post_status' => 'publish',
        'orderby' => 'title',
        'order' => 'ASC',
        'meta_query' => array(
            array(
                'key' => 'destination_state',
                'value' => ':"'.$state_id . '";' , // looking for serialized value in quotes
                'compare' => 'LIKE'
            )
        ),
    ) );