Support

Account

Home Forums General Issues Filtering with form that contains acf fields and its values from post

Unread

Filtering with form that contains acf fields and its values from post

  • Hello, so I am trying to make a filter which take all the values that acf fields have from all the posts and put them into the form. Its working , the form displays all the values that posts have and the filter works for some of the fields, radio and text fields. Checkboxes are making a big problem for me , so I am reaching out to you guys, Please help me 🙁
    THIS IS MY WEB SITE THE FORM IS LOCATED ON : https://croguide.eu/ma/
    THIS IS THE CODE:
    function acf_dropdown_form_shortcode() {

    // Get all posts
    $posts = get_posts(array(
    ‘posts_per_page’ => -1,
    ));

    // Get all ACF fields and values for each post
    $fields = array();
    foreach ($posts as $post) {
    $post_fields = get_fields($post->ID);
    if ($post_fields) {
    foreach ($post_fields as $name => $value) {
    if (!isset($fields[$name])) {
    $fields[$name] = array();
    }
    if (is_array($value)) {
    foreach ($value as $subvalue) {
    if (!in_array($subvalue, $fields[$name])) {
    $fields[$name][] = $subvalue;
    }
    }
    } else {
    if (!in_array($value, $fields[$name])) {
    $fields[$name][] = $value;
    }
    }
    }
    }
    }

    // If form is submitted, filter posts and display results
    if (isset($_POST[‘submit’])) {
    // Build meta query based on selected values
    $meta_query = array(‘relation’ => ‘AND’);
    foreach ($fields as $name => $values) {
    $selected_values = $_POST[$name];
    if (!empty($selected_values)) {
    // Check if field is a checkbox
    $field = get_field_object($name);
    if ($field[‘type’] == ‘checkbox’) {
    // Use OR operator for checkbox field
    $checkbox_query = array(‘relation’ => ‘OR’);
    foreach ($selected_values as $selected_value) {
    $checkbox_query[] = array(
    ‘key’ => $name,
    ‘value’ => ‘”‘ . $selected_value . ‘”‘,
    ‘compare’ => ‘LIKE’,
    );
    }
    $meta_query[] = $checkbox_query;
    } else {
    // Use = operator for other field types
    $meta_query[] = array(
    ‘key’ => $name,
    ‘value’ => $selected_values,
    ‘compare’ => ‘=’,
    );
    }
    }
    }
    var_dump($meta_query);
    // Query posts with meta query
    $filtered_posts = get_posts(array(
    ‘posts_per_page’ => -1,
    ‘meta_query’ => $meta_query,
    ));

    // Display results
    if ($filtered_posts) {
    $html = ‘<h3>Results:</h3>’;
    foreach ($filtered_posts as $post) {
    $html .= ‘<p>‘ . get_the_title($post) . ‘</p>’;
    }
    } else {
    $html = ‘<p>No results found.</p>’;
    }

    $html .= ‘<form method=”POST”><button type=”submit” name=”select-again”>Select Again</button></form>’;
    return $html;
    }

    // Create checkbox form

    // Create checkbox form
    $html = ‘<form method=”POST”>’;
    foreach ($fields as $name => $values) {
    $html .= ‘<fieldset>’;
    $html .= ‘<legend>’ . $name . ‘</legend>’;
    foreach ($values as $value) {
    $html .= ‘<label>’;
    $html .= ‘<input type=”checkbox” name=”‘ . $name . ‘[]” value=”‘ . $value . ‘”> ‘ . $value;
    $html .= ‘</label>’;
    }
    $html .= ‘</fieldset>’;
    }
    $html .= ‘<button type=”submit” name=”submit”>Submit</button>’;
    $html .= ‘</form>’;

    // Return form
    return $html;
    }
    add_shortcode(‘acf_dropdown_form’, ‘acf_dropdown_form_shortcode’);

    AND THIS IS PHP CODE OD MY ACF FIELD GROUP :
    if( function_exists(‘acf_add_local_field_group’) ):

    acf_add_local_field_group(array(
    ‘key’ => ‘group_64174a68b73f0’,
    ‘title’ => ‘globalno’,
    ‘fields’ => array(
    array(
    ‘key’ => ‘field_64174a690c055’,
    ‘label’ => ‘Availability’,
    ‘name’ => ‘availability’,
    ‘aria-label’ => ”,
    ‘type’ => ‘checkbox’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array(
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘only_front’ => 0,
    ‘frontend_admin_display_mode’ => ‘edit’,
    ‘no_values_message’ => ”,
    ‘choices’ => array(
    ‘Morning’ => ‘morning’,
    ‘Afternoon’ => ‘afternoon’,
    ‘Night’ => ‘night’,
    ‘Anytime’ => ‘anytime’,
    ),
    ‘default_value’ => array(
    ),
    ‘return_format’ => ‘value’,
    ‘allow_custom’ => 0,
    ‘layout’ => ‘vertical’,
    ‘toggle’ => 0,
    ‘save_custom’ => 0,
    ),
    array(
    ‘key’ => ‘field_64174aa00c056’,
    ‘label’ => ‘Location’,
    ‘name’ => ‘location’,
    ‘aria-label’ => ”,
    ‘type’ => ‘text’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array(
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘only_front’ => 0,
    ‘frontend_admin_display_mode’ => ‘edit’,
    ‘no_values_message’ => ”,
    ‘default_value’ => ”,
    ‘maxlength’ => ”,
    ‘placeholder’ => ”,
    ‘prepend’ => ”,
    ‘append’ => ”,
    ),
    array(
    ‘key’ => ‘field_64174aba0c057’,
    ‘label’ => ‘Price range’,
    ‘name’ => ‘price_range’,
    ‘aria-label’ => ”,
    ‘type’ => ‘radio’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array(
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘only_front’ => 0,
    ‘frontend_admin_display_mode’ => ‘edit’,
    ‘no_values_message’ => ”,
    ‘choices’ => array(
    ‘cheap’ => ‘cheap’,
    ‘mid’ => ‘mid’,
    ‘expensive’ => ‘expensive’,
    ),
    ‘default_value’ => ”,
    ‘return_format’ => ‘value’,
    ‘allow_null’ => 0,
    ‘other_choice’ => 0,
    ‘layout’ => ‘vertical’,
    ‘save_other_choice’ => 0,
    ),
    array(
    ‘key’ => ‘field_64174b030c058’,
    ‘label’ => ‘Type’,
    ‘name’ => ‘type’,
    ‘aria-label’ => ”,
    ‘type’ => ‘checkbox’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array(
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘only_front’ => 0,
    ‘frontend_admin_display_mode’ => ‘edit’,
    ‘no_values_message’ => ”,
    ‘choices’ => array(
    ‘restoran’ => ‘restoran’,
    ‘aktivnost’ => ‘aktivnost’,
    ),
    ‘default_value’ => array(
    ),
    ‘return_format’ => ‘value’,
    ‘allow_custom’ => 0,
    ‘layout’ => ‘vertical’,
    ‘toggle’ => 0,
    ‘save_custom’ => 0,
    ),
    array(
    ‘key’ => ‘field_64174b320c059’,
    ‘label’ => ‘Food’,
    ‘name’ => ‘food’,
    ‘aria-label’ => ”,
    ‘type’ => ‘checkbox’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array(
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘only_front’ => 0,
    ‘frontend_admin_display_mode’ => ‘edit’,
    ‘no_values_message’ => ”,
    ‘choices’ => array(
    ‘vegan’ => ‘vegan’,
    ‘nonharam’ => ‘nonharam’,
    ‘asian’ => ‘asian’,
    ‘local’ => ‘local’,
    ),
    ‘default_value’ => array(
    ),
    ‘return_format’ => ‘value’,
    ‘allow_custom’ => 0,
    ‘layout’ => ‘vertical’,
    ‘toggle’ => 0,
    ‘save_custom’ => 0,
    ),
    array(
    ‘key’ => ‘field_64174b660c05a’,
    ‘label’ => ‘Activity’,
    ‘name’ => ‘activity’,
    ‘aria-label’ => ”,
    ‘type’ => ‘checkbox’,
    ‘instructions’ => ”,
    ‘required’ => 0,
    ‘conditional_logic’ => 0,
    ‘wrapper’ => array(
    ‘width’ => ”,
    ‘class’ => ”,
    ‘id’ => ”,
    ),
    ‘only_front’ => 0,
    ‘frontend_admin_display_mode’ => ‘edit’,
    ‘no_values_message’ => ”,
    ‘choices’ => array(
    ‘nightlife’ => ‘nightlife’,
    ‘cultural’ => ‘cultural’,
    ‘adrenalin’ => ‘adrenalin’,
    ),
    ‘default_value’ => array(
    ),
    ‘return_format’ => ‘value’,
    ‘allow_custom’ => 0,
    ‘layout’ => ‘vertical’,
    ‘toggle’ => 0,
    ‘save_custom’ => 0,
    ),
    ),
    ‘location’ => array(
    array(
    array(
    ‘param’ => ‘post_type’,
    ‘operator’ => ‘==’,
    ‘value’ => ‘post’,
    ),
    ),
    ),
    ‘menu_order’ => 0,
    ‘position’ => ‘normal’,
    ‘style’ => ‘default’,
    ‘label_placement’ => ‘top’,
    ‘instruction_placement’ => ‘label’,
    ‘hide_on_screen’ => ”,
    ‘active’ => true,
    ‘description’ => ”,
    ‘show_in_rest’ => 0,
    ‘no_values_message’ => ”,
    ));

    endif;
    If you need anything else to help you fix this, ask.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.