0

Hi I have a cpt query which displays 1 latest event based on date. It works fine - problem is when date is < today (theres no future events to display) this query is empty and also breaks other queries on site.

<?php
    $today = date("Y-m-d");             
    $argsmi = array('posts_per_page' => 1, 
                    'post_type' => 'imprezy', 
                    'orderby' => 'meta_value_num', 
                    'order' => ASC, 
                    'meta_query' => array(
                    'relation' => 'OR',
                    array(
                        'key' => 'data_imprezy',
                        'value' => $today,
                        'compare' => '>=',
                        'type' => 'datetime'
                    ),
                    array(
                        'key' => 'data_imprezy_do',
                        'value' => $today,
                        'compare' => '>=',
                        'type' => 'datetime'
                    ))  
    );

$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $argsmi );
if ( $wp_query->have_posts() ) : ?>

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

div class="singleevthumbdiv">

<a href="<?php the_permalink(); ?>">

<?php
if ( has_post_thumbnail() ) {

the_post_thumbnail('thumbnail', array('class' => 'singleevthumb'));

}
else {
}
?>

</a>
<div class="cbmob"></div>

<div class="datatitle">

<a href="<?php the_permalink(); ?>" class="singleevtitle"><?php the_titlesmall('', '...', true, '25') ?></a><div style="clear:both;"></div>

<?php $date = DateTime::createFromFormat('Ymd', get_field('data_imprezy')); ?>

<?php $datedo = DateTime::createFromFormat('Ymd', get_field('data_imprezy_do')); ?>

<span class="data">

<?php echo $date->format('d-m-Y'); ?>

<?php if( get_field('data_imprezy_do') ): ?>

- <?php echo $datedo->format('d-m-Y'); ?>

<?php endif; ?>

</span> 

<div style="clear:both;"></div>

</div>

</div>

<?php endwhile; ?>                                              

<?php $wp_query = null; $wp_query = $temp; wp_reset_query(); ?>
<?php endif; ?> 

1 Answer 1

2

Before we start, you should properly indent your code and remove php spam. This makes your code much easier to read and understand. Secondly, rather use curlies ({}) instead of syntax like :, endif and endwhile. It is easier to debug and to read. Just another tip, comment your code for future reference, it makes it easier to remember what you did a month or a year ago

Your code should have looked something like this

<?php
    $today = date("Y-m-d");             
    $argsmi = array(
        'posts_per_page' => 1, 
        'post_type'      => 'imprezy', 
        'orderby'        => 'meta_value_num', 
        'order'          => ASC, 
        'meta_query'     => array(
            'relation'   => 'OR',
            array(
                'key'     => 'data_imprezy',
                'value'   => $today,
                'compare' => '>=',
                'type'    => 'datetime'
            ),
            array(
                'key'      => 'data_imprezy_do',
                'value'    => $today,
                'compare'  => '>=',
                'type'     => 'datetime'
            )
        )  
    );

    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query( $argsmi );
    if ( $wp_query->have_posts() ) { 

        while( $wp_query->have_posts() ) { 
        $wp_query->the_post(); ?>

            <div class="singleevthumbdiv">

                <a href="<?php the_permalink(); ?>">

                    <?php
                    if ( has_post_thumbnail() ) {

                        the_post_thumbnail('thumbnail', array('class' => 'singleevthumb'));

                    }
                    ?>

                </a>
                <div class="cbmob"></div>

                <div class="datatitle">

                    <a href="<?php the_permalink(); ?>" class="singleevtitle"><?php the_titlesmall('', '...', true, '25') ?></a><div style="clear:both;"></div>

                    <?php 
                        $date = DateTime::createFromFormat('Ymd', get_field('data_imprezy')); 

                        $datedo = DateTime::createFromFormat('Ymd', get_field('data_imprezy_do')); 
                    ?>

                    <span class="data">

                        <?php 
                            echo $date->format('d-m-Y'); 

                            if( get_field('data_imprezy_do') ){

                                echo '- ' . $datedo->format('d-m-Y');

                            } 
                        ?>

                    </span> 

                    <div style="clear:both;"></div>

                </div>

            </div>

        <?php 
        }                                              

        $wp_query = null; 
        $wp_query = $temp; 
        wp_reset_query(); 

    }
?> 

To get to your issue, using the $wp_query hack is quite dirty and something I really do not like. You should never mess with global values as this breaks the globals as you are setting foreign values to them. Even if you later reset the value, it is still a dirty hack. This is the same what query_posts does which you should never ever use

You should rather use custom variables outside of the global scope variables.

Based on the above, your code should look something like this

<?php
    $today = date("Y-m-d");             
    $argsmi = array(
        'posts_per_page' => 1, 
        'post_type'      => 'imprezy', 
        'orderby'        => 'meta_value_num', 
        'order'          => ASC, 
        'meta_query'     => array(
            'relation'   => 'OR',
            array(
                'key'     => 'data_imprezy',
                'value'   => $today,
                'compare' => '>=',
                'type'    => 'datetime'
            ),
            array(
                'key'      => 'data_imprezy_do',
                'value'    => $today,
                'compare'  => '>=',
                'type'     => 'datetime'
            )
        )  
    );

    $q = new WP_Query( $argsmi );
    if ( $q->have_posts() ) { 

        while( $q->have_posts() ) { 
        $q->the_post(); ?>

            <div class="singleevthumbdiv">

                <a href="<?php the_permalink(); ?>">

                    <?php
                    if ( has_post_thumbnail() ) {

                        the_post_thumbnail('thumbnail', array('class' => 'singleevthumb'));

                    }
                    ?>

                </a>
                <div class="cbmob"></div>

                <div class="datatitle">

                    <a href="<?php the_permalink(); ?>" class="singleevtitle"><?php the_titlesmall('', '...', true, '25') ?></a><div style="clear:both;"></div>

                    <?php 
                        $date = DateTime::createFromFormat('Ymd', get_field('data_imprezy')); 

                        $datedo = DateTime::createFromFormat('Ymd', get_field('data_imprezy_do')); 
                    ?>

                    <span class="data">

                        <?php 
                            echo $date->format('d-m-Y'); 

                            if( get_field('data_imprezy_do') ){

                                echo '- ' . $datedo->format('d-m-Y');

                            } 
                        ?>

                    </span> 

                    <div style="clear:both;"></div>

                </div>

            </div>

        <?php 
        } //endwhile                                              

        wp_reset_postdata(); 

    } //endif
?> 
2
  • Query reset was in wrong place - should be after endif
    – th3rion
    Oct 5, 2015 at 9:07
  • 1
    You shouldn't be using wp_reset_query(). Also, wp_reset_postdata() should go after endwhile but before endif Oct 5, 2015 at 9:09

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.