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="https://wordpress.stackexchange.com/questions/204600/<?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="https://wordpress.stackexchange.com/questions/204600/<?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="https://wordpress.stackexchange.com/questions/204600/<?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="https://wordpress.stackexchange.com/questions/204600/<?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
?>