最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Display posts order by custom post in a dropdown menu

matteradmin6PV0评论

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


}

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 edited May 29, 2013 at 12:01 Rarst 100k10 gold badges161 silver badges298 bronze badges asked May 29, 2013 at 10:06 LuisminLuismin 232 silver badges8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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  

}

Post a comment

comment list (0)

  1. No comments so far