Support

Account

Home Forums General Issues Dropdown box to list posts based on custom fields

Solved

Dropdown box to list posts based on custom fields

    • downer

    • July 16, 2013 at 8:53 pm

    Hello, I am trying to get posts based on ACFs by choosing from a select box.

    The form is like this`<?php ?>
    <form method=”post” action=”<?php echo $_SERVER['PHP_SELF']; ?>”>
    <select name=”my_city” id=”city” class=”postform” onchange=”submit();”>
    <option selected=”selected”>Choose a size</option>
    <option value=”la”>Los Angeles</option>
    <option value=”london”>London</option>
    </select>
    </form>`

    And Query is like this

    <?php 
     
    	// args
    	$city = isset($_GET['my_city']) ;
    
    	$args = array(
    		'numberposts' => -1,
    		'post_type' => 'jobs',
    		'meta_key' => 'city_country',
    		'meta_value' => $city
    	);
    	 
    	// get results
    	$the_query = new WP_Query( $args );
    	 
    	// The Loop
    	?>
    	<?php if( $the_query->have_posts() ): ?>
    		<ul>
    		<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    			<li>
    				<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    			</li>
    		<?php endwhile; ?>
    		</ul>
    	<?php endif; ?>
    	 
    	<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

    Yet I cant list posts on select box change it redirects me to the homepage. I have tried my different ways still no luck. Anyone care to help.

    Thanks

    • Elliot

    • July 17, 2013 at 2:27 pm

    Hi @downer

    I think your issue is a logic error in your code. The line:

    
    $city = isset($_GET['my_city']) ;
    

    Will set to true / false, not the value. Perhaps you mean to do this.

    
    $city = '';
    if( isset($_GET['my_city']) )
    {
    $city = $_GET['my_city'];
    }
    
    • downer

    • July 17, 2013 at 6:23 pm

    Thanks for the response, Elliot.

    I have created a separate PHP file to display results and it worked.

    I have used this

    $city = $_POST['citycountry'];

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.