0

Create multidimensional array of taxonomies

Hello all, I need to create a multidimensional array to populate my select via javascript. At the beginning are two selects of taxonomies, 'store' and 'state'. When 'store' is chosen, the second select is loaded with the terms 'state'

My current query is this. And it works perfectly, but I need to create an array with this information, can anyone help me?

$post_type = 'store';
$tax = 'store_type';

//$tax_terms = get_terms($tax);
$tax_terms = get_terms('store_type');
if ( $tax_terms ) {
    foreach ($tax_terms as $tax_term) {
        $args = array(
            'post_type' => $post_type,
            "$tax" => $tax_term->slug,
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'orderby' => 'title',
            'order' => 'ASC',
        );


        $my_query = new WP_Query($args);
    ?>

        <?php if ( $my_query->have_posts() ) : ?>
            <?php $list = $tax_term->name;?>

              <h2><?php echo $list; ?></h2> <!-- All store types are displayed -->

            <ol>
            <?php while ($my_query->have_posts()) : ?>
                <?php $my_query->the_post(); ?>
                <li>
                    <?php
                       $type_terms = wp_get_post_terms( $post->ID, 'state');
                        if ( $type_terms ) {

                            echo '<span>'.$type_terms[0]->name .'</span>'; // All terms that exist in the 'state' taxonomy are displayed. 
                        }
                    ?>
                </li>
            <?php endwhile; ?>
            </ol>
        <?php endif; ?>
    <?php
    }
}
wp_reset_postdata();

Put simply, I'm trying to do a json like this:

{"store_type": "xxx", "state": "ny"}, {"store_type": "yyy", "state": "ca"}

0

The following should work, if I understand what you want correctly.

Basically the only change is where you display the list items - instead of echoing it, you save it to the array. We also know the store type at that point, so we can save them both in their own array as a new element in the main array.

$all_store_info = array(); // Declare array to save the store info

$post_type = 'store';
$tax = 'store_type';

$tax_terms = get_terms('store_type');
if ( $tax_terms ) {
    foreach ($tax_terms as $tax_term) {

        $args = array([...]); // trimmed to make it easier to see changes
        $my_query = new WP_Query($args);

        if ( $my_query->have_posts() ) : 
            while ($my_query->have_posts()) : $my_query->the_post(); 

                $type_terms = wp_get_post_terms( $post->ID, 'state');
                if ( $type_terms ) {

                    /* Add this store type from $tax_term from the outer loop
                       and the first (presumably only) state to our array */
                    $all_store_info[] = array( 
                                            "store_type" = $tax_term->name,
                                            "state"      = $type_terms[0]->name
                                        );
                }

            endwhile; 
        endif;
    }
}
wp_reset_postdata();
2
  • This is perfect. Not exactly the way you wrote it, but it did help me solve the problem. Thank you! Oct 18 '17 at 15:36
  • @FagnerPelicioni Glad to help! The code you get here is never going to be exact because we won't know the details of what you need, but as long as you can work the rest out for yourself that's what matters :) Oct 18 '17 at 16:16

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.