How are you all. I am just struggling to make my custom select box keep retain select value in form when page is reload or user come on form via different navigation.

my select box is:

      $args = array('post_type'=> 'portfolio', 'posts_per_page' =>100, 'offset'=> 0);
      $myposts = get_posts( $args );
      foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
      <option value="<?php the_title(); ?>"><?php the_title(); ?></option>
      <?php endforeach;

in Core Php I know how to do this but in wordpress I am displaying post name in selectbox, so how can i achieve this, help would be appreciated....Thanks

  • 1
    You could use the_ID() as option value and get_queried_object_id() to look up in which post you are. – realloc Oct 29 '13 at 8:54
  • I am just beginner in wordpress, and love the way you provide info – Dk-Macadamia Oct 29 '13 at 10:25
  • Did you try the code I provided? – realloc Oct 29 '13 at 11:02

If I have understood correctly, you could use get_the_ID() and compare the value with get_queried_object_id() to archive this:

$args       = array( 'post_type'=> 'portfolio', 'posts_per_page' =>100, 'offset'=> 0 );
$myposts    = get_posts( $args );
$current_id = get_queried_object_id();
foreach ( $myposts as $post ) {
    setup_postdata( $post );
        ( $current_id == get_the_ID() ) ? ' selected="selected"' : '',

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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