WordPress: настраиваемая форма поиска на основе значений метабоксов - PullRequest
0 голосов
/ 28 января 2019

Мне нужна помощь в понимании того, как создать пользовательскую форму поиска на сайте WordPress.

Я создал метабокс.Это выпадающий список.Моя цель - предоставить пользователю возможность поиска сообщений по категориям (используя метабокс категории по умолчанию) и по районам.Вот метабокс округа в functions.php файле.

add_action( 'add_meta_boxes','init_metabox' );
function init_metabox() {
    add_meta_box( 'arr_type', 'Arrondissement et type', 'arr_type', 'post', 'side' );

}

function arr_type( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'paris_meta_box_nonce' );
    $arrondissement = get_post_meta( $post->ID, 'arrondissement', true );

    echo '<label for="arrondissement">Définir un arrondissement</label>';
    echo '<select name="arrondissement">';
        echo '<option ' . selected( '1er arrondissement', $arrondissement, false ) . ' value="1er arrondissement">1er arrondissement</option>';
        echo '<option ' . selected( '2ème arrondissement', $arrondissement, false ) . ' value="2ème arrondissement">2ème arrondissement</option>';
        echo '<option ' . selected( '3ème arrondissement', $arrondissement, false ) . ' value="3ème arrondissement">3ème arrondissement</option>';
        echo '<option ' . selected( '4ème arrondissement', $arrondissement, false ) . ' value="4ème arrondissement">4ème arrondissement</option>';
        echo '<option ' . selected( '5ème arrondissement', $arrondissement, false ) . ' value="5ème arrondissement">5ème arrondissement</option>';
        echo '<option ' . selected( '6ème arrondissement', $arrondissement, false ) . ' value="6ème arrondissement">6ème arrondissemen</option>';
        echo '<option ' . selected( '7ème arrondissement', $arrondissement, false ) . ' value="7ème arrondissement">7ème arrondissement</option>';
        echo '<option ' . selected( '8ème arrondissement', $arrondissement, false ) . ' value="8ème arrondissement">8ème arrondissement</option>';
        echo '<option ' . selected( '9ème arrondissement', $arrondissement, false ) . ' value="9ème arrondissement">9ème arrondissement</option>';
        echo '<option ' . selected( '10ème arrondissement', $arrondissement, false ) . ' value="10ème arrondissement">10ème arrondissement</option>';
        echo '<option ' . selected( '11ème arrondissement', $arrondissement, false ) . ' value="11ème arrondissement">11ème arrondissement</option>';
        echo '<option ' . selected( '12ème arrondissement', $arrondissement, false ) . ' value="12ème arrondissement">12ème arrondissement</option>';
        echo '<option ' . selected( '13ème arrondissement', $arrondissement, false ) . ' value="13ème arrondissement">13ème arrondissement</option>';
        echo '<option ' . selected( '14ème arrondissement', $arrondissement, false ) . ' value="14ème arrondissement">14ème arrondissement</option>';
        echo '<option ' . selected( '15ème arrondissement', $arrondissement, false ) . ' value="15ème arrondissement">15ème arrondissement</option>';
        echo '<option ' . selected( '16ème arrondissement', $arrondissement, false ) . ' value="16ème arrondissement">16ème arrondissement</option>';
        echo '<option ' . selected( '17ème arrondissement', $arrondissement, false ) . ' value="17ème arrondissement">17ème arrondissement</option>';
        echo '<option ' . selected( '18ème arrondissement', $arrondissement, false ) . ' value="18ème arrondissement">18ème arrondissement</option>';
        echo '<option ' . selected( '19ème arrondissement', $arrondissement, false ) . ' value="19ème arrondissement">19ème arrondissement</option>';
        echo '<option ' . selected( '20ème arrondissement', $arrondissement, false ) . ' value="20ème arrondissement">20ème arrondissement</option>';
    echo '</select>';

}

add_action( 'save_post','save_metabox' );
function save_metabox( $post_id ) {
    // verify meta box nonce
    if ( !isset( $_POST['paris_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['paris_meta_box_nonce'], basename( __FILE__ ) ) ) {
        return;
    }

    // return if autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user's permissions.
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }
    if ( isset( $_POST['arrondissement'] ) ) {
        update_post_meta( $post_id, 'arrondissement', $_POST['arrondissement'] );
    }

}

Для каждого поста, с этим метабоксом, я определил район для поста.

Теперь мне нужно создать такой же выпадающий списокв моей настраиваемой поисковой форме, чтобы позволить пользователю выполнять поиск по району.

<form method="get" id="advanced-searchform" role="search" action="<?php echo esc_url( home_url( '/' ) ); ?>">

    <h3><?php _e( 'Rechercher votre balade', 'paris' ); ?></h3>

    <!-- PASSING THIS TO TRIGGER THE ADVANCED SEARCH RESULT PAGE FROM functions.php -->
    <input type="hidden" name="s" value="">

    <p>
        <label for="arrondissement">Choisissez votre arrondissement</label>    
        <select name="arrondissement" id="arrondissement">
            <option value="1er arrondissement">1er arrondissement</option>';
            <option  value="2ème arrondissement">2ème arrondissement</option>';
            <option  value="3ème arrondissement">3ème arrondissement</option>';
            <option  value="4ème arrondissement">4ème arrondissement</option>';
            <option  value="5ème arrondissement">5ème arrondissement</option>';
            <option  value="6ème arrondissement">6ème arrondissemen</option>';
            <option  value="7ème arrondissement">7ème arrondissement</option>';
            <option  value="8ème arrondissement">8ème arrondissement</option>';
            <option  value="9ème arrondissement">9ème arrondissement</option>';
            <option  value="10ème arrondissement">10ème arrondissement</option>';
            <option  value="11ème arrondissement">11ème arrondissement</option>';
            <option  value="12ème arrondissement">12ème arrondissement</option>';
            <option  value="13ème arrondissement">13ème arrondissement</option>';
            <option  value="14ème arrondissement">14ème arrondissement</option>';
            <option  value="15ème arrondissement">15ème arrondissement</option>';
            <option  value="16ème arrondissement">16ème arrondissement</option>';
            <option  value="17ème arrondissement">17ème arrondissement</option>';
            <option  value="18ème arrondissement">18ème arrondissement</option>';
            <option  value="19ème arrondissement">19ème arrondissement</option>';
            <option  value="20ème arrondissement">20ème arrondissement</option>';
        ;?>
        </select>
    </p>

    <p>
        <label for="type">Que voulez-vous voir ?</label>
        <?php wp_dropdown_categories( 'show_option_none=Select' ); ?>
    </p>
    <input type="submit" id="searchsubmit" value="Search" />
</form>

Итак, как мне сделать поисковый запрос, чтобы учесть выбор пользователя, категории и района ??Вот моя pre_gest_post функция.Я знаю, что что-то упустил, но не знаю что.Может кто-нибудь помочь мне, пожалуйста ??

function search_filter( $query ) {
    if ( !is_admin() && $query->is_main_query() ) {
        if ( $query->is_search ) {
            $meta_query = array();

            if ( isset( $_POST['arrondissement']) && $_POST['arrondissement'] ) {

                $meta_query[] = array(
                  'key'     => 'arrondissement',
                  'value'   => $_POST['arrondissement'],
                  'compare' => '=',
                );

            }

            $cat = empty( $_GET['cat'] ) ? '' : (int) $_GET['cat'];
            add_query_arg( 'cat', $cat );

            $query->set('meta_query', $meta_query);
        }
    }
}

add_filter( 'pre_get_posts', 'search_filter' );
...