Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.
$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>';
        endforeach;

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>
<option>Bangalore</option>
<option>Delhi</option>

can anyone help me....

share|improve this question
add comment

2 Answers

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;
        endforeach;
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
add comment

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

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

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

share|improve this answer
add comment

Your Answer

 
discard

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.