Confusing title but using the Types toolset I added the two letter state code (CA, TX, FL, etc...) to each of my 'Store' custom post type. The following query will order all my stores by state code, in desc/asc order:

$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',

My question: if I have 10 stores in California, is there a way that, before the first store, I print out a heading (in this case, California)? So if i have states in CA, TX, and FL, before my block of stores in CA it'll say 'Califoarnia', before my block of stores in FL it'll say 'Florida', etc...?

And if so, is there a way to add a dynamic class to each heading? Something like <h2 class="<?php echo $state_code; ?>-state">California</h2> which would output ca-state as the class?


You could try a concept like the following (post your loop and I will improve the model):

$state_group = 'Open Loop';

// start the loop
// if posts then while

$state_code = // lookup this post state code
if ( $state_code != $state_group ){
    echo $state_code; 
    $state_group = $state_code;

//end loop

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.