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:
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(). ?>
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.
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:
For a good understanding of how to create a front end search engine, try this:
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!
You must be logged in to reply to this topic.
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!
© 2019 Advanced Custom Fields. Subscribe