WordPress-Form不过滤分类结果

时间:2019-09-26 作者:AMCode

我创建了一个插件,该插件使用一个数组,特定于我要为哪些帖子提供分类法,以便创建自定义类别,该分类法的名称是:categoria

我们讨论的是事件预订,因此很明显,每个事件都可以不同于其他事件,每个事件都可以通过meta键的meta\\u值中包含的日期进行搜索:metakey_AMC_data

示例:

形式:

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

        <input type="hidden" name="search" value="advanced">
        <input
                type="text"
                name="date-picker-example"
                id="date-picker-example"
                class="text-white form-control datepicker"
        >
    <input class="btn btn-sm btn-info" type="submit" id="searchsubmit" value="Cerca" />
</form>
输出数据:

<?php
// Get data from URL into variables
$_name = $_GET[\'date-picker-example\'];

$today = date(\'Ymd\');
// Start the Query
$v_args = array(
    \'post_type\'     =>  array(\'eventi-suite\'), // your CPT
    \'numberpost\'    => -1,
    \'meta_value\'    =>  $_name, // looks into everything with the keyword from your \'name field\'
    \'order\'      => \'ASC\',
    \'orderby\' => \'metakey_AMC_data\',
    \'meta_query\' => array(
        array(
            \'key\'     => \'metakey_AMC_data\',
            \'compare\'   => \'>=\',
            \'value\'     => $today,
        ),
    ),

);
$vehicleSearchQuery = new WP_Query( $v_args );

// Open this line to Debug what\'s query WP has just run
//var_dump($vehicleSearchQuery->request);

// Show the results
if( $vehicleSearchQuery->have_posts() ) :
    while( $vehicleSearchQuery->have_posts() ) : $vehicleSearchQuery->the_post();
        ?><div style="padding-top: 25px;" class="col-md-6"><!-- Card -->
        <!-- Card -->
        <?php


        include(\'content/home_page/card.php\');

        ?>
        <!-- Card -->
        <!-- Card -->
        <?php
        // Assumed your cars\' names are stored as a CPT post title
        ?></div><?php
    endwhile;
else :
    ?><div style="text-align: center;padding-top: 25px;" class="col-md-12"><?php
    _e( \'Ci dispiace, non abbiamo nessun evento da proporti nella data che hai immesso. <br> Prova con un altra data.\', \'textdomain\' );
    ?></div><?php
endif;
wp_reset_postdata();
?>
在这里一切都很好。

url示例:

https://sitename.xxx/?search=advanced&date-picker-example=30-09-2019
现在开始解决问题,因为我在表单中实现了关于分类法的部分

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

        <input type="hidden" name="search" value="advanced">
        <input
                type="text"
                name="date-picker-example"
                id="date-picker-example"
                class="text-white form-control datepicker"
        >
        <label class="text-white" for="date-picker-example">Seleziona data</label>
        <?php

        /* NEW PART */

        $terms = get_terms(array(
            \'taxonomy\' => \'categoria\',
            \'hide_empty\' => false,
        ));
        ?>
        <select
                name="categoria_evento"
                id="categoria_evento"
                class="browser-default custom-select">
            <option disabled selected>Categorie Eventi</option>
            <?php
            foreach ($terms as $term)
            {
                echo "<option value=\'{$term->slug}\'>{$term->name}</option>";
            }

            /* END NEW PART */

            ?>
        </select>
    </div>

    <input class="btn btn-sm btn-info" type="submit" id="searchsubmit" value="Cerca" />
</form>
输出日期:

<?php
// Get data from URL into variables
$_name = $_GET[\'date-picker-example\'];
$categoria = $_GET[\'categoria_evento\'];
$today = date(\'Ymd\');
// Start the Query
$v_args = array(
    \'post_type\'     =>  array(\'eventi-suite\'), // your CPT
    \'posts_per_page\'    => -1,
    \'meta_value\'    =>  $_name, // looks into everything with the keyword from your \'name field\'
    \'order\'      => \'ASC\',
    \'orderby\' => \'metakey_AMC_data\',
    \'meta_query\' => array(
        array
        (
            \'key\'     => \'metakey_AMC_data\',
            \'compare\'   => \'>=\',
            \'value\'     => $today,

        ),
    ),
    \'tax_query\' => get_terms(array
    (
        array
        (
            \'taxonomy\' => \'categoria\',
            \'field\'    => \'slug\',
            \'terms\'    => $categoria, /*RICHIAMO LA VARIABILE DEL FORM TASSONOMIE*/
            \'hide_empty\' => false,
        )
    )

));
$vehicleSearchQuery = new WP_Query( $v_args );

// Open this line to Debug what\'s query WP has just run
print_r($vehicleSearchQuery->request);

// Show the results
if( $vehicleSearchQuery->have_posts() ) :
    while( $vehicleSearchQuery->have_posts() ) : $vehicleSearchQuery->the_post();
        ?><div style="padding-top: 25px;" class="col-md-6"><!-- Card -->
        <!-- Card -->
        <?php


        include(\'content/home_page/card.php\');

        ?>
        <!-- Card -->
        <!-- Card -->
        <?php
        // Assumed your cars\' names are stored as a CPT post title
        ?></div><?php
    endwhile;
else :
    ?><div style="text-align: center;padding-top: 25px;" class="col-md-12"><?php
    _e( \'Ci dispiace, non abbiamo nessun evento da proporti nella data che hai immesso. <br> Prova con un altra data.\', \'textdomain\' );
    ?></div><?php
endif;
wp_reset_postdata();
?>
但我得到的结果是,它只按日期过滤我,而不按分类法过滤我,因此,通过输入例如2019年9月30日和开胃酒分类法,它会显示我输入日期的所有事件,但显示所有分类法的所有事件,为什么?

url示例:

https://namesite.xxx/categoria/aperitivi/?search=advanced&date-picker-example=30-09-2019
<小时>

1 个回复
最合适的回答,由SO网友:Milan Hirpara 整理而成

请尝试以下代码,以便使用日期和类别进行搜索。

$_name = $_GET[\'date-picker-example\'];
$categoria = $_GET[\'categoria_evento\'];
$today = date(\'Ymd\');
// Start the Query
$v_args = array(
    \'post_type\'     =>  array(\'eventi-suite\'), // your CPT
    \'posts_per_page\'    => -1,
    \'meta_value\'    =>  $_name, // looks into everything with the keyword from your \'name field\'
    \'order\'      => \'ASC\',
    \'orderby\' => \'metakey_AMC_data\',
    \'meta_query\' => array(
        array
        (
            \'key\'     => \'metakey_AMC_data\',
            \'compare\'   => \'>=\',
            \'value\'     => $today,

        ),
    ),
    \'tax_query\' => array(
        array(
          \'taxonomy\' => \'categoria\',
          \'field\' => \'slug\',
          \'terms\' => $categoria
        )
    );

));
$vehicleSearchQuery = new WP_Query( $v_args );

相关推荐

Make taxonomy query dynamic

我对多个页面使用相同的页面模板。-> page-base.php在使用此模板的每个页面上,我想显示使用CPT.我的问题是,如何使数组动态化,以自动更改“分类法”?这是我当前的代码<?php $terms = get_terms( array( \'taxonomy\' => \'catmaison\', \'hide_empty\' => false,