Support

Account

Home Forums Front-end Issues Query posts based on radio button field Reply To: Query posts based on radio button field

  • Hi @bounty_digital

    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