0

I am making an event booking, obviously the events can be different and so I created taxonomies to associate the events with the latter.

Being events as properties of the post they have enclosed within a meta_value that corresponds to the meta key:metakey_AMC_data the date of the event.

It made me a little mad to order the events from the smallest to the largest on the taxonomy page but in the end I did it.

$v_args = array(
    'post_type'     =>  array('eventi-suite'), // your CPT
    'posts_per_page'    => -1,
    'orderby' => 'metakey_AMC_data',
    'meta_query' => array(
        array
        (
            'key'     => 'metakey_AMC_data',
            'compare'   => '>=',
        ),
    ),
    'tax_query' => array(
        array(
            'taxonomy' => 'categoria',
            'field' => 'slug',
            'terms' => 'aperitivi'
        )
    )

);
$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();
        ?>
        <?php
        /*CONVERTIRE LA DATA DEGLI EVENTI SALVATA NEL DB DA DD-MM-YY A Y-M-D*/
        $var = get_post_meta(get_the_ID(),'metakey_AMC_data',true);
        $date = str_replace('/', '-', $var);
        $data_eventi = date('Y-m-d', strtotime($date));

        /*PRENDERE LA DATA CORRENTE IN FORMATO Y-m-d DIRETTAMENTE DA MYSQL*/
        $data_mysql = date( 'Y-m-d', current_time( 'timestamp', 0 ) );

        if ($data_eventi <= $data_mysql): ?>
        <!-- NON MOSTRARE NULLA -->
        <?php else: ?>
            <div style="padding-top: 25px;" class="col-md-4"><!-- Card -->
                <?php include('content/home_page/card.php');  ?>
            </div>
        <?php endif; ?>

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

after performing the conversion of the date in the form of a string inside the db, and taking the current date mysql, I implemented an if loop, where I tell it that if the event is less than the current date it should not be shown if it is greater it must to be shown.

Where's my problem?

The fact is that I have 4 events with different dates:

  • 09/30/2019
  • 09/30/2019
  • 10/8/2019
  • 10/10/2019

that should come out in this order, instead they come out in this other

  • 09/30/2019
  • 09/30/2019
  • 10/10/2019
  • 10/08/2019

how do I place a sort from the smallest to the largest within my if loop, to solve this?

  • maybe storing the date as unix timestamp will save you a lot of problems: it's a number, then you can render it as date with the format you like – alo Malbarez Sep 29 '19 at 17:11

Your Answer

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

Browse other questions tagged or ask your own question.