0

I've created a "stores" custom post type and on the page I order it by title: $args = array( 'post_type' => 'store','posts_per_page' => 100, 'orderby' => 'title', 'order' => "ASC" );

I'm using the Types toolset and I've created a field for the admin to add the state in two letter form (CA, NY, TX...). I am trying to modify my loop to not only order by state, but also order by title and I'm not sure how to accomplish that. I looked around and it appears I can make the orderby parameter an array. Below it my code:

$state_code = types_render_field("state-code", array());
$args = array( 
    'post_type' => 'store',
    'posts_per_page' => 100, 
    'orderby' => array(
        $state_code => 'ASC',
        'title' => 'ASC',
    ),
);

Am I able to order it in the following hierarchy?: 1) state code alphabetical order (AK, AL, AR....), then by 2) title

0

if you created the custom field using wp-Type then you would have to use wpcf-state-code

$state_code = types_render_field("state-code", array());
$args = array( 
    'post_type' => 'store',
    'posts_per_page' => 100, 
    'orderby' => array( 'ASC' => 'DESC', 'meta_value' => 'ASC' )
    'meta_key' => 'wpcf-state-code',
);

This should work, let me know.

  • This worked! Thank you. You forgot a comma at the end of the orderyby key so that threw me off for a little bit. Thank you. – VCP Dec 5 '16 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.