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

  • 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

  • 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'];
    }
    
  • 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)

The topic ‘Dropdown box to list posts based on custom fields’ is closed to new replies.