Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In my WordPress backend I have some custom posts and, in one of them, I want to display in a dropdown field some posts related with but ordered by post type. Iwant to display something like this.

<select multiple="multiple" style="height:200px; width:300px">
<optgroup label="Productos">
<option>Producto 1</option>
<option>Producto 2</option>
<option>Producto 3</option>
<option>Producto 4</option>
<option>Producto 5</option>
<option>Producto 6</option>
</optgroup>
<optgroup label="Paso 1">
<option>Subpagina 1</option>
<option>Subpagina 2</option>
<option>Subpagina 3</option>
</optgroup>
</optgroup>
<optgroup label="Paso 2">
<option>Subpagina 1</option>
<option>Subpagina 2</option>
<option>Subpagina 3</option>
</optgroup>
</optgroup>
<optgroup label="Paso 3">
<option>Subpagina 1</option>
<option>Subpagina 2</option>
<option>Subpagina 3</option>
</optgroup>
</select>

Here is the code that displays all of posts that I want but without order. I figure out that I have to use the id of the custom post type but I do not how to:

{
// Add the Meta Box  

function add_custom_meta_box_related() {  
    add_meta_box(  
        'custom_meta_box_related', // $id  
        'Related Information', // $title   
        'show_custom_meta_box_related', // $callback  
        'related', // $page  
        'normal', // $context  
        'high'); // $priority  
}  
add_action('add_meta_boxes', 'add_custom_meta_box_related');

    // Field Array  
    $prefix_related = 'custom_';  
    $custom_meta_fields_related = array(  

        array(  
            'label' => 'Related Items',  
            'desc' => 'Select a related item(s)',  
            'id'    =>  $prefix_materiales.'post_id',  
            'type' => 'post_list',  
            'post_type' => array('products','paso1','paso2','paso3','paso4','compra'),  
)
    );  

    // The Callback  
function show_custom_meta_box_related() {  
global $custom_meta_fields_related, $post;  
// Use nonce for verification  
echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';  

    // Begin the field table and loop  
    echo '<table class="form-table">';  
    foreach ($custom_meta_fields_related as $field_related) {  
        // get value of this field if it exists for this post  
        $meta_related = get_post_meta($post->ID, $field_related['id'], true);  
        // begin a table row with  
        echo '<tr> 
                <th><label for="'.$field_related['id'].'">'.$field_related['label'].'</label></th> 
                <td>';  
                switch($field_related['type']) {  
                    // case items will go here 

                    // post_list  
                    case 'post_list':  
                    $items = get_posts( array (  
                        'post_type' => $field_related['post_type'],  
                        'posts_per_page' => -1  
                    ));  
                    echo '<select multiple name="'.$field_related['id'].'" id="'.$field_related['id'].'"> 
                    <option value="">Select One or more</option>'; // Select One  
                    foreach($items as $item) {  
                        echo '<option value="'.$item->ID.'"',$meta_related == $item->ID ? ' selected="selected"' : '','> '.$item->post_title.'</option>';  
                    } // end foreach  
                    echo '</select><br /><span class="description">'.$field_related['desc'].'</span>';  
                    break;
                } //end switch  
        echo '</td></tr>';  
    } // end foreach  
    echo '</table>'; // end table  
}

// Save the Data  
function save_custom_meta_related($post_id) {  
    global $custom_meta_fields_related;  

    // verify nonce  
    if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))   
        return $post_id;  
    // check autosave  
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
        return $post_id;  
    // check permissions  
    if ('page' == $_POST['post_type']) {  
        if (!current_user_can('edit_page', $post_id))  
            return $post_id;  
        } elseif (!current_user_can('edit_post', $post_id)) {  
            return $post_id;  
    }  

    // loop through fields and save the data  
    foreach ($custom_meta_fields_related as $field_related) {  
        $old = get_post_meta($post_id, $field_related['id'], true);  
        $new = $_POST[$field_related['id']];  
        if ($new && $new != $old) {  
            update_post_meta($post_id, $field_related['id'], $new);  
        } elseif ('' == $new && $old) {  
            delete_post_meta($post_id, $field_related['id'], $old);  
        }  
    } // end foreach  
}  
add_action('save_post', 'save_custom_meta_related');


}
share|improve this question

1 Answer 1

Now I can order de post_types. Any help for the multiple save?

// The Callback  

function show_custom_meta_box_related() {
global $custom_meta_fields_related, $post;
// Use nonce for verification
echo '';

// Begin the field table and loop  
echo '<table class="form-table">';  
foreach ($custom_meta_fields_related as $field_related) {  
    // get value of this field if it exists for this post  
    $meta_related = get_post_meta($post->ID, $field_related['id'], true);  
    // begin a table row with  
    echo '<tr> 
            <th><label for="'.$field_related['id'].'">'.$field_related['label'].'</label></th> 
            <td>';  
            switch($field_related['type']) {  
                // case items will go here 

                // post_list  
                case 'post_list': 
                echo '<select multiple style="height:200px; width:300px" name="'.$field_related['id'].'" id="'.$field_related['id'].'">';
                echo '<option value=""></option>'; // Select One
                foreach($field_related['post_type'] as $tipo_post){
                $items = get_posts( array (  
                    'post_type' => $tipo_post,  
                    'posts_per_page' => -1  
                )); 

                foreach($items as $item) {  
                    echo '<option value="'.$item->ID.'"',$meta_related == $item->ID ? ' selected="selected"' : '','> '.$item->post_title. '</option>';  
                } // end foreach

                }
                echo '</select><br /><span class="description">'.$field_related['desc'].'</span>';  
                break;
            } //end switch  
    echo '</td></tr>';  
} // end foreach  
echo '</table>'; // end table  

}

share|improve this answer
    
Any one? I can not find the way. –  Luismin Jun 4 '13 at 8:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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