Set default option in dropdown of WP_Query?

(Revised answer)

If I understand it properly, try these:

  1. Define $the_post for each “property”:

    <?php $the_post = get_post(); ?>
    <div class="property-details">
        <h2><?php the_title(); ?></h2>
        ...
    </div>
    
  2. Then change the drop-down code to:

    <select name="propname">
        <?php
            $found = wp_list_filter( $the_query->posts, [ 'ID' => $the_post->ID ] );
            if ( empty( $found ) ) :
            ?>
                <option selected><?php echo get_the_title( $the_post ); ?></option>
            <?php
            endif;
    
            if($the_query->have_posts()){
                while($the_query->have_posts()) : $the_query->the_post(); ?>
                    <option<?php selected( get_the_ID(), $the_post->ID ); ?>><?php the_title(); ?></option>
                <?php endwhile;
            } wp_reset_query();
        ?>
    </select>
    

Leave a Comment