Home › Forums › Front-end Issues › 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.
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
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
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!
The topic ‘How to query select dropdown menu to build a search engine’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.