Home › Forums › Front-end Issues › Query posts based on radio button field › Reply To: Query posts based on radio button field
You can render out the radio buttons dynamically, by loading the field object and looping through the $field[‘choices’].
Check out the get_field_object function to learn how to render out choices. This will take care of any ‘user created’ options.
As for the archive page, that part is easy.
WP has a filter called ‘pre_posts’. I highly encourage you to research this filter and understand how you can hook in and modify the WP_Query args before the query has been run.
What you can do is add some code to your functions.php file which will hook into this filter and look for a $_GET parameter. This $_GET parameter will come from the url that you send the user to after they have selected a value from the radio button.
This $_GET param can have any name, but needs to have the value of the selected radio button.
Back to the filter, look for this $_GET param and if it exists, use it to add a meta_key and meta_value args to the WP_Query args.
Your archive page will then dynamically use the URL param to update the results all on it’s own!
Easy!
Thanks
E
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.