问题描述
通过短代码,我将以下代码放置在侧栏中以显示随机即将发生的事件。目前,它会随机显示所有事件。如何更改代码以仅显示比当前日期年轻的事件?
<?PHP
// Build a custom query to get posts from future dates.
$querystr = `
SELECT wposts.*
FROM $wpdb->posts wposts,$wpdb->postMeta wpostMeta
WHERE wposts.ID = wpostMeta.post_id
AND wpostMeta.Meta_key = 'f21_event_startdate'
AND STR_TO_DATE(wpostMeta.Meta_value,'j. M Y') >= CURDATE()
AND wposts.post_status = 'publish'
AND wposts.post_type = 'events'
ORDER BY STR_TO_DATE(wpostMeta.Meta_value,'j. M Y') RAND
LIMIT 1
`;
$events = $wpdb->get_results($querystr,OBJECT);
if ($events):
global $post;
foreach ($events as $post):
setup_postdata($post); ?>
<?PHP the_field('f21_event_startdate'); ?>
<h2><a style="color:#1e73be;" href="<?PHP the_permalink() ?>"><?PHP the_title(); ?></h2>
<div style="margin-top:10px; margin-bottom: 10px;"><?PHP the_post_thumbnail('full'); ?></div></a>
<b><?PHP the_field('f21_event_sub_header'); ?></b>
<?PHP endforeach;
else : ?>
<li>Sorry,no events coming up.</li>
<?PHP endif; ?>
解决方法
您可以WP_Query()。检查下面的代码。
$args = array(
'post_type' => 'events','posts_per_page' => -1,'meta_key' => 'f21_event_startdate','meta_value' => date('Ymd'),'meta_compare' => '>=','orderby' => 'rand','order' => 'ASC'
);
$upcoming_events = new WP_Query( $args );