0

I created a plugin that uses an array, specific to which posts I want to give my taxonomy so that I can create custom categories, the name of this taxonomy is: categoria

We are talking about an event booking so obviously each event can be different from the other, each event can be searched by the date that is enclosed within the meta_value of the meta key:metakey_AMC_data

example:

form:

<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>

output data:

<?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();
?>

and up here everything works perfectly.

example of url:

https://sitename.xxx/?search=advanced&date-picker-example=30-09-2019

now begin problem, because i implemented in the form the part about the taxonomy

<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>

output date:

<?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();
?>

but the result I get is that it only filters me by date, but it doesn't filter me by taxonomy, so by entering for example 09/30/2019 and aperitiv taxonomy, it shows me all the events of the date I entered but of all the taxonomies, why?

example url:

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

1 Answer 1

0

Try below code for search using date and category.

$_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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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