Query not running as expected

Hi all,

I’m currently having issues running queries based on the Date Picker field and comparing this with the current date. I’ve opted to set the Return Format to “U” so that it returns a string which I can then compare to a string version of the current date.

What I would like to do is display content on certain pages based on this comparison (greater than current date for “coming soon” and also the opposite for “now showing”). However, it seems that the data is getting very mixed up as I have content that should be on “now showing” which is displaying on “coming soon” instead.

In addition I have the page split so that I can use an ACF to set certain films to be “featured” and therefore not show up in the lower results as well.

Below is the code I have so far and a live deployment can be viewed on the following.

<?php
get_header();
?>
<!-- START - Coming Soon -- Header -->
<section class="comingsoon-header">
    <h1 class="section__heading">Coming Soon</h1>
    <!-- START - Coming Soon Slider -->
    <?php
    $dateToday = date("U"); ?>
    <p><?php echo $dateToday ?></p>
<?php
    $comingSoonFilms = new WP_Query(array(
        'post_type' => 'film',
        'posts_per_page' => -1,
        'order' => 'ASC',
        'meta_query' => array(
            array(
                'key' => 'film_releasedate',
                'value' => $dateToday,
                'type' => 'DATE',
                'compare' => '>',
            ),
            array(
                'key' => 'film_active',
                'value' => '1',
            ),
        )
    ));
    ?>
    <?php if ($comingSoonFilms->have_posts()) : ?>
        <!-- START - Coming Soon Slider -->
        <div class="section__slider-container text-center">
            <div class="swiper swiper-container swiper--section swiper--section--filmswide swiper--section--filmswide--comingsoon">
                <div class="swiper-wrapper">
                    <!-- Slides -->

                    <?php while ($comingSoonFilms->have_posts()) {
                        $comingSoonFilms->the_post();

                        if (get_field('film_comingsoon_featured_status') == true) {
                    ?>

                            <div class="swiper-slide">
                                <a href="<?php the_permalink(); ?>">
                                    <img src="<?php the_field('film_comingsoon_featured_image'); ?>" alt="" title="" class="swiper__imgbg" />
                                    <div class="comingsoon-header__details">
                                        <p class="comingsoon-header__details__heading"><?php the_title() ?> </p>
                                        <span class="cta">View More</span>
                                        <p class="comingsoon-header__releasedate"><i class="fas fa-clock"></i>Coming: <?php echo date('d/m/Y', get_field('film_releasedate')) ?><br />
                                        <br /><?php echo get_field('film_releasedate') ?></p>
                                    </div>
                                </a>
                            </div>
                    <?php
                        }
                    }
                    ?>


                </div>
                <div class="swiper-button-prev"></div>
                <div class="swiper-button-next"></div>
            </div>

        </div>
        <!-- END - Coming Soon Slider -->
</section>
<!-- START - Coming Soon -- More -->
<section>
    <h2 class="section__heading">More Coming Soon</h2>
    <div class="filmpanels filmpanels--grid">
        <?php while ($comingSoonFilms->have_posts()) {
            $comingSoonFilms->the_post();
            if (get_field('film_comingsoon_featured_status') == false) {
        ?>
                <div>
                    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                        <img src="<?php echo wp_get_attachment_url(get_field('film_poster')); ?>" alt="" title="" class="swiper__imgbg" />
                        <p><?php the_title(); ?></p>
                        <?php echo date('d/m/Y', get_field('film_releasedate')) ?>
                        <p><?php echo get_field('film_releasedate') ?></p>
                    </a>
                </div>

        <?php
            }
        }
        ?>
    </div>
</section>
<section class="results--pagination">
    <?php

        echo paginate_links(array(
            'total' => $comingSoonFilms->max_num_pages
        ));

        wp_reset_query();

    ?>
<?php else : ?>
    <p>No films currently coming soon!</p>
<?php endif; ?>
</section>
<?php
get_footer();
?>

Thanks in advance

Ok so on review I think it must be something to do with the conditions within the query I’m running as I’ve double-checked the Return Format of the ACF and it’s set to a Custom value of “U” which should match back to my dateToday variable value.

$comingSoonFilms = new WP_Query(array(
        'post_type' => 'film',
        'posts_per_page' => -1,
        'order' => 'ASC',
        'meta_query' => array(
            array(
                'key' => 'film_releasedate',
                'value' => $dateToday,
                'type' => 'DATE',
                'compare' => '>',
            ),
            array(
                'key' => 'film_active',
                'value' => '1',
            ),
        )
    ));

As you can see from the live deployment, I’ve printed both the formatted and unformatted data for the release date. Therefore I don’t think this is related to the Advanced Custom Field itself.

Any thoughts please or things I could try to debug the issue with?

Ok, so after some thought I had a feeling that I had to specify that I wanted to have both queries return true for it to display the relevant data. I adjusted my query to include a “relation” but still the incorrect results are showing for Coming Soon, and no results are pulling through for Now Showing.

Below are the amended queries for each page:

Coming Soon

 $comingSoonFilms = new WP_Query(array(
        'post_type' => 'film',
        'posts_per_page' => -1,
        'order' => 'ASC',
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'film_releasedate',
                'value' => $dateToday,
                'type' => 'DATE',
                'compare' => '>',
            ),
            array(
                'key' => 'film_active',
                'value' => '1',
            ),
        )
    ));

Now Showing

 $nowShowingFilms = new WP_Query(array(
        'post_type' => 'film',
        'posts_per_page' => -1,
        'order' => 'DESC',
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'film_releasedate',
                'value' => $dateToday,
                'type' => 'DATE',
                'compare' => '<',
            ),
            array(
                'key' => 'film_active',
                'value' => '1',
            ),
        )
    ));

If any other details would be helpful let me know!

Thanks in advance.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.