Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

Luismin on "Order posts by custom post type in dropdown field"

$
0
0

Hi commnunity!

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. I want 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, any idea?

Note: I`ll like to save more than one post at the same time.

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

Thanks in advance


Viewing all articles
Browse latest Browse all 8245

Trending Articles