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!
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!
🎉ACF has made it into the latest round of the @TheTorqueMag Plugin Madness!— Advanced Custom Fields (@wp_acf) March 22, 2023
🙏 Please swing by and vote for ACF in the latest matchup.
🙌 Each round lasts one week, but you can vote twice each week. Make sure to get both your votes in!https://t.co/iTpRE82O1k
© 2023 Advanced Custom Fields.