在查询中过期wordpress帖子 - 而不是循环

时间:2011-06-28 13:15:58

标签: wordpress

我目前正在使用以下代码在循环中自定义字段“date”之后过期并隐藏帖子(工作正常):

<?php
while (have_posts()) : the_post();
//to check against expiration date;
$currentdate = date("Ymd");
$expirationdate = get_post_custom_values('date');
if (is_null($expirationdate)) {
$expirestring = '30005050'; //MAKE UN-EXPIRING POSTS ALWAYS SHOW UP;
} else {
if (is_array($expirationdate)) {
$expirestringarray = implode($expirationdate);
}
$expirestring = str_replace("/","",$expirestringarray);
} //else
if ( $expirestring >= $currentdate ) { 
// post loop contents
} ?>

我想过滤掉查询中的帖子 - 这是可能的。我有一个插件,从查询结果生成谷歌地图,而不是循环 - 任何人有任何想法?这是我当前的查询

<?php query_posts($query_string.'&order=asc&orderby=meta_value&meta_key=date');
if (have_posts()) : ?>

我忘了提到结果帖子也应该按照与现在最近的日期出现的相同日期字段进行排序。

1 个答案:

答案 0 :(得分:0)

试试这个:

<?php

 $querystr = "
    SELECT wposts.* 
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id 
    AND wpostmeta.meta_key = 'date' 
    AND wpostmeta.meta_value < CURRENT_DATE
    AND wposts.post_status = 'publish' 
    AND wposts.post_type = 'post' 
    ORDER BY wposts.post_date DESC, wpostmeta.meta_value DESC
 ";

 $pageposts = $wpdb->get_results($querystr, OBJECT);

 ?>

我假设元键'date'的元值以mysql格式('YYYY-MM-DD')存储。如果没有,您可以尝试使用mysql date time functions对其进行格式化。

然后,您可以使用以下代码显示帖子。

 <?php if ($pageposts): ?>
 <?php global $post; ?>
 <?php foreach ($pageposts as $post): ?>
 <?php setup_postdata($post); ?>
 <h2><?php echo $post->title; ?></h2>
 <?php endif; ?>