I want to make the user the ability to chose several posts that related to the post that his about the publish, so that post will shown at the bottom of the post itself.
So I created a metabox with multiple selctions.
Now I have two problems:
1)Save his chooses as array of options. I’ve manage to do it only with this code:
update_post_meta( $post_id, 'related-posts', array_map( 'strip_tags', $_POST['related-posts']) );
The thing is that it makes array-of-array:
array (size=1)
0 =>
array (size=8)
0 => string '38262' (length=5)
1 => string '38257' (length=5)
2 => string '38247' (length=5)
3 => string '38217' (length=5)
4 => string '38228' (length=5)
5 => string '38229' (length=5)
6 => string '38211' (length=5)
7 => string '38198' (length=5)
Is this the right way of doing it? there’s no better solution to make it only one-dimensional array? (I don’t want to use implode method).
2)When the choises were saved, they need to be highlighted, so I’ve tried this:
$array_posts = get_posts($args); ?>
<form action="" method="post" name="related-post-form">
<select name='related-posts[]' multiple='multiple' size='30'>
<?php foreach($array_posts as $post): setup_postdata($post); ?>
<option value="<?php echo $post->ID; ?>" <?php if ($_POST['related-posts'] == $post->ID ) echo ' selected'; ?>><?php the_title(); ?></option>
<?php endforeach; ?>
</select>
</form>
Doesn’t work.
Any help will be appreciate.. Thanks!