Support

Account

Home Forums Front-end Issues How to query select dropdown menu to build a search engine

Solved

How to query select dropdown menu to build a search engine

  • Hey Elliot. I need to create a Query based on a dropdown menu that sets a var equal to the selected option.

    I’ve used this example of yours below and it has worked for a simple example of another Custom Post Type I have but in the case of the “Search Engine” I need to create, the user will select two dropdown menus and once these are set and the search button clicked, the Query needs to run.

    Problem I am having is that the meta_value looks like this in the database:
    a:1:{i:0;s:9:”Merlot”;}

    How do I access the “Merlot” in this, what I assume is an array?

    <?php 
     
    // args
    $args = array(
    	'numberposts' => -1,
    	'post_type' => 'wines',
    	'meta_key' => 'type',
    	'meta_value' => $wine //this needs to be Merlot from the dropdown menu
    );
     
    // 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(). ?>

    Many thanks.

    • James

    • January 30, 2014 at 1:32 pm

    Hi @sixfootjames

    Is the drop down box an ACF selection?

    If so, the currently selected element can be accessed by using

    $wine = get_field('name-of-drop-down-box');
    before defining $args

    Cheers

  • Hi Elliot. Thanks for this.

    I realise my next question is not related to this one but, let me take a step back and first ask, how do I generate the select html tag based on the ACF selection I have created?

    Based on all my findings, I will document all the results in this post on How to create a search engine using Advanced Custom Fields in the hope that it might help someone else understand this.

    Cheers

    • James

    • January 31, 2014 at 5:25 am

    Hi @sixfootjames,

    Just so you know. This is not Elliot, we are here to help him with his super popular plugin. ๐Ÿ™‚

    I would consider doing this using jQuery and AJAX. You could use ACF to generate your original select and a blank select.

    And then write some generic PHP to accept an AJAX request with the selection and then return a data set to populate the second select.

    IMO, this would make for a better UX.

  • I misunderstood the post below and still in need of some help with this please.

    Thanks guys. I managed to get a little further with this from this great article I found about dynamically populating select fieldโ€™s choice in Advanced Custom Fields

    • Elliot

    • February 12, 2014 at 9:51 am

    Hi @sixfootjames

    To respond to your original question, to query a value which is saved as an array, you will need to perform a LIKE compare. You can read about this here:
    http://www.advancedcustomfields.com/resources/field-types/checkbox/

    For a good understanding of how to create a front end search engine, try this:
    http://www.advancedcustomfields.com/resources/tutorials/creating-wp-archive-custom-field-filter/

    Thanks
    E

  • Good man, thanks so much Elliot. It helps when you use the right search terms. I would never have thought to use Archive as a “Search Engine” term when I normally google something like

    http://site:advancedcustomfields.com search engine

    Thanks for this much appreciate!

Viewing 7 posts - 1 through 7 (of 7 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.