0

I have a select dropdown in my post meta in which I am getting post ids of all post of another custom post type. see below code-

    <select name="property_type" id="property_type">
               <option value=""></option>
             <?php
                $propertyType_args=array(
                  'post_type' => "soto_propertyType",
                  'post_status' => 'publish',
                  'posts_per_page' => -1,
                  'order' =>'ASC',
                  );

               $propertyType_query = null;
               $propertyType_query = new WP_Query($propertyType_args);
               if( $propertyType_query->have_posts() ) {
                  while ($propertyType_query->have_posts()) : $propertyType_query->the_post(); 
             ?>
                  <option value="<?php echo the_ID(); ?>" <?php selected( $property_type, the_ID(), false ); ?>><?php echo the_title(); ?></option>
            <?php
                  endwhile;
                }
                wp_reset_query(); 
              ?>
</select>

In this select list I am getting all ids of all post but not getting existing value as selected in option. I am getting select dropdown like this-

enter image description here

What I have to do to show existing value as selected in dropdown list. Please help me.

1

the_ID() echoes its value, you can't use it in that context. Change it to get_the_ID().

Many functions in WordPress follow this pattern- one function will print, while the same function with get_ prepended will return its value.

  • @shashank Please revisit this answer and accept it as it seems that the solution worked. :-) – Pieter Goosen Mar 3 '15 at 18:04

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.