• Hi all,

    I’ve a metabox for a specific custom post type (Stores), inside the metabox I create a variable amount of checkboxes based on a WP_Query. In other words I have many checkboxes as the children pages (Armani, Gucci, Versace, …) of a specific page (Brands).

    function add_stores_metabox() {
    
        add_meta_box(
            'store-brands',
            'Store Brands',
            'display_brands_stores_metabox',
            'stores',
            'side',
            'high'
        );
    
    }
    
    function display_brands_stores_metabox() {
    
        $args = array(
            'post_type' => 'page',
            'post_parent' => 43
            );
    
        $brands_in_stores = new WP_Query($args);
    
        while ($brands_in_stores->have_posts()) : $brands_in_stores->the_post();
    
            global $post;
    
            wp_nonce_field( plugin_basename( __FILE__ ), 'brands_stores_nonce' );
    
            $title = get_the_title();
            $html = '<p>';
            $html .= '<input id="' . $post->post_name . '" type="checkbox" name="' . $post->post_name . '" value=""/>';
            $html .= '<label for="' . $post->post_name . '">' . $title . '</label>';
            $html .= '</p>';
    
            echo $html;
    
        endwhile; wp_reset_query();
    }

    Now the UI works fine, but I don’t know how to save these data looping through an array, because I’ve to find dynamically the name of the field to use the update_post_meta() in the saving function that will follow.

    Someone could helps me?

  • The topic ‘Save Metabox Data Looping Through an Array’ is closed to new replies.