0

Edit: I have an archive page for a Custom Post Type. Each article that appears on this page has information entered via Advanced Custom Fields about that article, and a button that opens a modal window (hidden div in the loop, made to open a php mailer form). Inside that modal window, is a dropdown.

The dropdown that populates Custom Post Type titles as an option. It uses WP Query to pull those titles from Wordpress.

code for the CPT details section and modal open button

<div class="property-details">
            <h2><?php the_title(); ?></h2>
            <?php echo get_field('property_address');?>

            <!-- excerpt -->
            <?php if( '' != get_the_excerpt() || '' != get_the_content() ) { ?>
                <?php do_action('layers_before_list_post_content'); ?>
                <?php do_action('layers_list_post_content'); ?>
                <?php do_action('layers_after_list_post_content'); ?>
            <?php } ?>
        </div>

<a href="#open-modal-form-<?php echo $post->ID; ?>" class="button inquire-button">Inquiry</a>

code for the modal section:

<div id="open-modal-form-<?php echo $post->ID; ?>" class="modal-window">
            <div>
                <a href="#" title="close" class="modal-close"><i class="eicon-close"></i></a>
                <!-- I couldn't get contact form 7 to play nice, so I made my own form. Recipient Email is managed in inquiry.php. -->
                <form action="<?php bloginfo('template_directory'); ?>/inquiry.php" method="post">
                    <input type="text" name="fname" placeholder="First name">
                    <input type="text" name="lname" placeholder="Last name">

                    <?php 
                        $args = array(
                            'post_type' => 'property',
                            'tax_query' => array(
                                array(
                                    'taxonomy' =>'project_category',
                                    'field' => 'slug',
                                    'terms' => array('current-projects','past-projects')
                                )
                            ),
                        );

                        $the_query = new WP_Query($args);
                    ?>
                    <select name="propname">
                        <?php
                            if($the_query->have_posts()){
                                while($the_query->have_posts()) : $the_query->the_post(); ?>
                                    <option><?php the_title(); ?></option>
                                <?php endwhile; 
                            } wp_reset_query();
                        ?>
                    </select>

                    <input type="email" name="email" placeholder="Email">

                    What type of lease are you looking for?
                    <input type="radio" name="lease-type" value="Rental" checked>Rental
                    <input type="radio" name="lease-type" value="Commercial">Commercial

                    <textarea name="comments" placeholder="Message"></textarea>
                    <input type="submit" value="Send Request">
                </form>
                <p><small>DB Services does not collect any personally identifiable information about you when you visit the Website unless you voluntarily provide this information, for example by contacting us through our email forms (including sending us queries or responding through the Website to our job postings.) Personal information collected in these cases may include your name, contact details, email address, telephone number and your resume.</small></p>
            </div>
        </div>

How do I have it so that on the page I am currently on (with one of the CPT modal windows in the dropdown) is the one in the dropdown already selected?

0

(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>
    
  • It didn't work unfortunately. Let me explain how the page is set up, I think it may need more clarification. I will edit original post. – Xero1 Aug 14 at 2:38
  • Can you show your full code including the modal stuff? Or where can I see the code? – Sally CJ Aug 14 at 3:07
  • I have updated the original post with the modal div code. – Xero1 Aug 14 at 3:58
  • I've just updated the answer - let me know if that works? – Sally CJ Aug 14 at 6:09
  • 1
    That worked! Thank you! – Xero1 Aug 14 at 14:16

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.