• Hello,

    I have a custom content type ‘products’. Instead of using the custom taxonomies, I decided to store the Category and Subcategory as Post Meta. What I want to do is create a drop down menu with categories and subcategories:

    global $post;
    $categories= array();
    
    $product_categories = new WP_Query();
    $product_categories->query('post_type=products&posts_per_page=10000');
    while($product_categories->have_posts()) : $product_categories->the_post();
    $post_id= $post->ID;
    $category= get_post_meta($post_id, 'category', true);
    $subcategory= get_post_meta($post_id, 'subcategory', true);
    $categories[$category]= array();
    array_push($categories[$category], $subcategory);
    endwhile;
    print_r($categories);

    The above code will output:

    Array ( [Plastic Liners & Film] => Array ( [0] => Steel Pail Liners ) [Stormwater Management] => Array ( [0] => Drain Guards ) [Spill Containment] => Array ( [0] => Spill Pallets ) [Sorbents & Spill Kits] => Array ( [0] => Oil-Only Sorbents ) [Drums & Containers] => Array ( [0] => Steel Pails ) [HazMat Containers] => Array ( [0] => Accessories ) )

    It does what I want, but it will only add one subcategory, even though there are several. How can I get around this?

    Much thanks…

  • The topic ‘Multidimensional Array Question’ is closed to new replies.