WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
$term_id .= $_POST['main_brand_id'];
        $term = get_term( $term_id, 'state' );
        $slug = $term->slug;
        global $post;
        $args = array( 'post_type' =>'dealers','taxonomy'=>'state', 'term' => $slug );
        $myposts = get_posts( $args );
        foreach( $myposts as $post ) :  setup_postdata($post);
            echo '<option>';
                $options .= '<option value="'.$post->id.'">';
                $options .=  the_title();
            echo '</option>';

I have to pass the post id to get the output as below

<option value="24">Andhra Pradesh</option>
<option value="29">Bangalore</option>
<option value="44">Delhi</option>

This piece of code is not working........

$options .= '<option value="'.$post->id.'">';

This is the output im getting

<option>Andhra Pradesh</option>

can anyone help me....

share|improve this question
up vote 1 down vote accepted

$post->id needs to be $post->ID

You can also use get_the_ID()

Since your calling setup_postdata() you need to call wp_reset_postdata() after your foreach loop.

You also need to echo the value of $options somewhere.

foreach( $myposts as $post ) :  setup_postdata($post);
                $options .= '<option>';
                $options .= '<option value="'.$post->id.'">';
                $options .=  get_the_title();
                $options .= '</option>';
                    echo $options;
share|improve this answer
Chris_O thanks for the response, tried it, but didn't worked. – Solomon Henry May 25 '12 at 12:03
Can you add var_dump( $post ); somewhere in your foreach loop and report back what the output is? – Chris_O May 25 '12 at 12:22
Also where are you echoing the value of $options? the_title prints to the screen so that could be your problem. – Chris_O May 25 '12 at 12:23
actually i have to echo like this-----> echo '<option value="-1" selected="selected">Select Post</option>'.$options; but its not working,..... yes chris u r right, the post title prints in the drop down and when i check in the page source i dont get the post id in the value attribute.... – Solomon Henry May 25 '12 at 12:29

i did like this chris.........

foreach( $myposts as $post ) :  setup_postdata($post);
        echo '<option value="'.$post->id.'">';
            $options .=  the_title();
        echo '</option>';

and its working, Thank you soo much Chris for ur awesome help............

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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