Hard Question - Wordpress does not filter the posts well by date

喜欢而已 提交于 2019-12-11 19:10:39

问题


I state that we are talking about an event booking, so each event has two features by default:

  • The date. which is enclosed within the meta_value given by the meta key:metakey_AMC_data

  • Custom taxonomy

Now the code I'm about to insert refers to the page of a taxonomy x, where all the events that have that taxonomy are displayed, and are sorted by date, which is recorded in the db format (dmy) and I have converted to (ymd) format

Code to show all events that have a current date:

<?php
/*MOSTRA EVENTI CON DATA CORRENTE*/

$args = array (
    'post_type' => get_option('customer_postquery'),
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key'       => 'metakey_AMC_data',
        )
    ),
    'tax_query' => array(
        array(
            'taxonomy' => 'categoria',
            'field' => 'slug',
            'terms' => $queried_object,
        )
    )
);
$custom_query = new WP_Query( $args );
?>

<?php if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post(); ?>

    <?php
    $var = get_post_meta(get_the_ID(),'metakey_AMC_data',true);
    $data_dell_evento = date("Y-m-d", strtotime( $var ));

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

    // You can eliminate comparison here as we are fetching only future events by query.
    if ($data_di_oggi === $data_dell_evento  ): ?>
        <div style="padding-top: 25px;" class="col-md-4"><!-- Card -->
            <?php include('content/home_page/card.php');  ?>
        </div>
    <?php  else: ?>

    <?php  endif; ?>


<?php
endwhile;
else :
    ?>

<?php
endif;
wp_reset_postdata();
/*MOSTRA EVENTI CON DATA CORRENTE*/
?>

Where: 'post_type' => get_option('customer_postquery'),it would be possible to dynamically recover all the custom post types that have been created.

Code to show all events that have a future date to the current one:

<!-- MOSTRA EVENTI CON DATA FUTURA A QUELLA CORRENTE -->
<?php
$v_args = array(
    'post_type' => get_option('customer_postquery'),
    'posts_per_page'    => -1,
    'order' => 'ASC',
    'meta_query' => array(
        array (
            'key'       => 'metakey_AMC_data',

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

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


if( $vehicleSearchQuery->have_posts() ) :
    while( $vehicleSearchQuery->have_posts() ) : $vehicleSearchQuery->the_post();
        ?>
        <?php
        $var = get_post_meta(get_the_ID(),'metakey_AMC_data',true);
        $data_dell_evento = date("Y-m-d", strtotime( $var ));

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

        if ($data_dell_evento > $data_di_oggi   ): ?>

            <div style="padding-top: 25px;" class="col-md-4"><!-- Card -->
                <?php include('content/home_page/card.php');  ?>
            </div>
        <?php  endif; ?>

    <?php
    endwhile;
else :
    ?>

<?php
endif;
wp_reset_postdata();
?>

Where is the problem?

The problem is that as long as there is a custom post type inside Wordpress and therefore in the query we would have for example:post_type' => ('NAMECUSTOMPOSTTYPE'),

All posts that are created are sorted perfectly, so if I have 4 posts:

  • Two with the current date

  • One on 08/10/2019

  • One by 10/10/2019

They are shown in order and perfect.

But of course as I said using: 'post_type' => get_option('customer_postquery'),

which dynamically recovers all the custom post types that have been created, an unpleasant situation arises, where:

Cpt 1:

  • post with current date (10/03/2019)

  • post with date (10/10/2019)

Cpt 2:

  • post with date (10/4/2019)

the order i have is

03/10

10/10

08/10

and this is not good

how can i put this right?

来源:https://stackoverflow.com/questions/58224214/hard-question-wordpress-does-not-filter-the-posts-well-by-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!