Skip to main content
Bumped by Community user
added 496 characters in body
Source Link
Rarst
  • 99.9k
  • 10
  • 161
  • 298

`

Producto 1Producto 2Producto 3Producto 4Producto 5Producto 6Subpagina 1Subpagina 2Subpagina 3Subpagina 1Subpagina 2Subpagina 3Subpagina 1Subpagina 2Subpagina 3`
<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');

{
// 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 '';

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;

}

// 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');


}

}
add_action('save_post', 'save_custom_meta_related');

}

`

Producto 1Producto 2Producto 3Producto 4Producto 5Producto 6Subpagina 1Subpagina 2Subpagina 3Subpagina 1Subpagina 2Subpagina 3Subpagina 1Subpagina 2Subpagina 3`

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 '';

// 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');

}

<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');


}
Source Link
Luismin
  • 23
  • 2
  • 8

Display posts order by custom post in a dropdown menu

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.

`

Producto 1 Producto 2 Producto 3 Producto 4 Producto 5 Producto 6 Subpagina 1 Subpagina 2 Subpagina 3 Subpagina 1 Subpagina 2 Subpagina 3 Subpagina 1 Subpagina 2 Subpagina 3 `

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 '';

// 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');

}