Wordpress使用基于表单参数的新WP_Query重新加载页面

时间:2017-03-22 06:42:57

标签: php wordpress html5 post get

我正在为我执行WP查询的页面做一个模板,然后,我显示查询的帖子。我想更改查询和显示的帖子取决于三个过滤器的信息,首选,而不重新加载整个页面,但是,现在,可以从那开始。我还读到,正确的方法是使用admin-post.php,但我无法做到。这是我到目前为止的表单代码:

<form method="get" action="<?php echo esc_url( admin_url('admin-post.php') ); ?> id="talentum-search-form">
    <div class="row">
        <div class="col-xs-4">
            <select class="form-control" id="talentum-alumni-state" name="talentum-alumni-statex" onchange='document.getElementById("talentum-search-form").submit();'>
                <option>Todos los estados</option>
                <?php $tmp_alumni_state = talentum_get_custom_field_values ( 'talentum-embajadores-estado' );
                if ( !empty( $tmp_alumni_state ) ):
                    foreach ( $tmp_alumni_state as $state): ?>
                        <option><?php echo $state ?></option>
                    <?php endforeach;
                endif; ?>
            </select>
        </div>
        <div class="col-xs-4">
            <select class="form-control" id="talentum-alumni-generation" name="talentum-alumni-generationx" onchange='document.getElementById("talentum-search-form").submit();'>
                <option>Todas las generaciones</option>
                <option>1ra. generación</option>
                <option>2da. generación</option>
            </select>
        </div>
        <div class="col-xs-4">
            <input class="form-control mr-sm-2" name="talentum-alumni-name" name="talentum-alumni-namex" type="text" placeholder="Nombre" onchange='document.getElementById("talentum-search-form").submit();'>
        </div>
        <input type="hidden" name="action" value="alumni_search">
    </div></form>

foreach调用一个函数并显示第一个选择的所有选项,而第二个选择只有三个选项。另一方面,这是我的函数附加到admin-post挂钩:

function talentum_alumni_search() {
    echo $_POST . "-";
    echo $_GET . "-";
    echo $_REQUEST . "-";
    echo $url = get_site_url() . "/talentum-universidad/egresados";

}
add_action( 'admin_post_nopriv_alumni_search', 'talentum_alumni_search' );
add_action( 'admin_post_alumni_search', 'talentum_alumni_search' );

现在,我只是回应变量,但它们是空的。此外,我使用重定向进行测试,但我意识到整个重定向过程太慢,无法将过滤器应用到同一页面。

你能告诉我在将GET或POST参数传递给函数方面我做错了什么吗?另外,你能告诉我一个快速的方法来重新加载页面并保持“Wordpress兼容”的参数吗?

更新:

我正在看变量并使用它们来符合WP_Query的参数。将查询发送回参数来自的页面的最正确方法是什么,以便使用此新查询重新加载页面?

这是我的疑问:

    $tmp_alumni_search_metas = [];

    if ( isset($_GET['as_nonce']) && wp_verify_nonce( $_GET['as_nonce'], 'alumni_search_') ):
        $args_custom_alumni_search  =   array(
            'posts_per_page'        =>  -1,
            'category'              =>  'Embajadores'
        );
        if ( !empty( $_GET['alumni-state'] ) && $_GET['alumni-state'] != 'Todos los estados' ):
            $safe_alumni_state = $_GET['alumni-state'];
            $tmp_alumni_search_metas['meta_alumni_state_clause'] = array(   'key'       => 'talentum-egresado-estado',
                                                                            'compare'   => '=',
                                                                            'value'     =>  $safe_alumni_state );
        endif;
        if ( !empty( $_GET['alumni-generation'] ) && $_GET['alumni-generation'] != 'Todas las generaciones' ):
            $safe_alumni_generation = $_GET['alumni-generation'];
            $tmp_alumni_search_metas['meta_alumni_generation_clause'] = array(  'key'       => 'talentum-egresado-generacion',
                                                                            'compare'   => '=',
                                                                            'value'     =>  $safe_alumni_generation );
            if ( isset( $tmp_alumni_search_metas ) ):
                $tmp_alumni_search_metas['relation'] = 'AND';
            endif;
        endif;
        $args_custom_alumni_search['meta_query'] = $tmp_alumni_search_metas;
        if ( !empty( $_GET['alumni-name'] ) ):
            $safe_alumni_name = $_GET['alumni-name'];
            $args_custom_alumni_search['s'] = $safe_alumni_generation;
        endif;
        var_dump( $args_custom_alumni_search );
    else:
        echo "¡Parámetro de seguridad incorrecto! (nonce)";
    endif;

0 个答案:

没有答案