1

I'm completely stuck and would very much appreciate some pointers/advise.

The problem: I want to create a search function that will let me filter Japanese onsen by their amenities, e.g. parking, sauna, bedrock bath, etc. which are stored as metadata in the post (a checklist).

I have registered these amenities as a metadata checklist in the post with the following code:

add_filter( 'rwmb_meta_boxes', 'onsen_register_meta_boxes' );
function onsen_register_meta_boxes( $meta_boxes ) {
    $meta_boxes[] = array(
        'title' => esc_html__( 'Amenities', 'onsen' ),
        'context' => 'side',
        'fields' => array(
            // CHECKBOX LIST
            array(
                'id'      => "{$prefix}checkbox_list",
                'type'    => 'checkbox_list',
                // Options of checkboxes, in format 'value' => 'Label'
                'options' => array(
                    'sauna' => esc_html__( 'Sauna', 'onsen' ),
                    'parking' => esc_html__( 'Parking', 'onsen' ),
                    'natural' => esc_html__( 'Natural', 'onsen' ),
                    'food' => esc_html__( 'Food', 'onsen' ),
                    'station' => esc_html__( 'Close to Station', 'onsen' ),
                    'towel' => esc_html__( 'Towel', 'onsen' ),
                    'bedrock' => esc_html__( 'Bedrock Bath', 'onsen' ),
                    'shampoo' => esc_html__( 'Shampoo', 'onsen' ),
                    'rest' => esc_html__( 'Rest Area', 'onsen' ),
                    'tattoo' => esc_html__( 'Tattoo OK', 'onsen' ),
                ),
            ),

        ),
    );
    return $meta_boxes;
}

This code gives me the checklist in the back-end and so I can easily check the relevant boxes for a given onsen. I understand that I need to create a front-end user form with a checklist of all the amenities, but I do not know how to create the $args for the WP_Query on a custom search page because as it's a checklist there may be multiple or no data points to grab... Any help would be very much appreciated.

1

Have you looked into the shortcode API? It has some very powerful tools to add as many array items as you'd like, and put the shortcode anywhere on your site.

Here is a small demo of what you could do:

function create_shortcode( $atts, $content = null ) {
     $a = shortcode_atts( array(
           'sauna' => 'attribute1',
           'parking' => 'attribute 2 default',
           'natural' => 'attribute 2 default',
           'food' => 'attribute 2 default'
   ), $atts );

    foreach ( $a as $key => $value ) {
         echo '<input type="checkbox" value="' . esc_attr($key) . '"' . $key. '>' . $value . "<br>";
    }

  }

add_shortcode( 'japan', 'create_shortcode');

Now to utilize this shortcode just enter this text [japan class="headline"]asdf[/japan] and it will produce a column of checkboxes. Not pretty but should get you in the right direction.

  • Thanks, but creating the list is less of a problem--I will only have it on one play on the website so I don't mind creating the form manually. The problem I'm having is how to write the WP_Query that will take the inputs from the URL when the user submits to a search page that displays only those results with an AND relationship. – Gecko Boy Oct 15 '16 at 11:07
1

I recently had a similar problem. I solved it by giving each checkbox a unique value (SQL row ID, page/post ID, etc) which you probably will have to set yourself, and when the user checks certain ones those checked boxes are sent to an SQL row. Make sure to have the name attribute on the checkboxes set to name="onsen_boxes[]" (to store these checkboxes entries as an array), then do something like this:

function collect_checkbox_IDs() {
    if (!isset( $_POST['onsen_boxes'])) {
        return;
    }

    $checkboxIdArray = array();
    foreach ( $_POST['onsen_boxes'] as $key) {
        $checkboxIdArray [] = $key;
    }

    return json_encode( $checkboxIdArray );
}

That will take the fields the user has selected and turn them into a JSON encoded array. When you want to use the values in this new array, you can just do echo json_decode($checkboxIdArray) to retrieve the individual items selected by the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.