使用自定义字段查询当前日期月份的帖子?

时间:2010-11-15 09:21:31

标签: wordpress

如何使用自定义字段查询具有当前日期月份的帖子:

这是我的代码

    <?php
    global $wp_query;
    $event_month = get_post_meta($wp_query->post->ID, 'eventdate', true); //format in db 11/15/2010
    $event_month = date("n"); //A numeric representation of a month, without leading zeros (1 to 12)
    $today= getdate(); ?>

    <?php query_posts('meta_key='.$event_month .'&meta_compare&meta_value=' .$today["mon"]);?>

<?php while (have_posts()): the_post(); ?>
    <div class="event-list-txt">
      <h4><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark">
        <?php the_title(); ?>
        </a></h4>
      <?php the_content(); //Display the content?>
    </div>
<?php endwhile; ?>
<?php wp_reset_query();?>

但是它没有显示输出..我想要显示的是当月的事件列表..例如,如果是11月,它将显示11月的事件列表,当12月的事件列表到来时它将显示显示12月的事件列表等等。事件列表日期附带自定义字段格式,如2010年11月15日与当前日期相比

我堆叠在上面..谢谢你们。

这是额外的预期输出 当前日期是11月,事件列表应如下所示:

+-----------------------+-------------+
| Event Name            | Date        |
+-----------------------+-------------+
| ABC Market opening    | 11/18/2010  | 
| ABC Market Seminar    | 11/25/2010  | 
| ABC Market Promo      | 11/29/2010  | 
+-----------------------+-------------+ 

1 个答案:

答案 0 :(得分:0)

我认为你的问题在这里:

 <?php query_posts('meta_key='.$event_month .'&meta_compare&meta_value=' .$today["mon"]);?>

...您正在尝试查找名为&#34; 11&#34;的自定义字段。 (11月的数字表示),值为11.

检查http://codex.wordpress.org/Function_Reference/query_posts#Time_Parameters是否有允许您设置&#34; WHERE&#34;的代码段。查询的一部分:

返回2009年3月1日至3月15日的帖子帖子:

<?php
//Create a new filtering function that will add our where clause to the query
function filter_where($where = '') {
  //posts for March 1 to March 15, 2009
  $where .= " AND post_date >= '2009-03-01' AND post_date < '2009-03-16'";
  return $where;
}
// Register the filtering function
add_filter('posts_where', 'filter_where');
// Perform the query, the filter will be applied automatically
query_posts($query_string);
?>

希望这有帮助...