如何存储事件日期:使用默认值post_date
文章的字段还是自定义元字段?
如果使用默认值post_date
, 您可以使用过滤器posts_where
要添加搜索条件,请执行以下操作:
// 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, 2010
$where .= " AND post_date >= \'2010-03-01\' AND post_date < \'2010-03-16\'";
return $where;
}
add_filter( \'posts_where\', \'filter_where\' );
$query = new WP_Query( $query_string );
remove_filter( \'posts_where\', \'filter_where\' );
有关更多示例,请查看
Codex.
如果使用自定义字段,我想您必须编写自己的自定义MySQL查询。这有点复杂。下面是一个示例:
global $wpdb;
$post_ids = $wpdb->get_col( "
SELECT ID FROM {$wpdb->posts}
JOIN {$wpdb->postmeta}
WHERE ID=post_id
AND meta_key=\'your_custom_meta_key\'
AND meta_value >= \'2010-03-01\'
AND meta_value < \'2010-03-16\'
" );
foreach ( $post_ids as $post_id ) {
$post = get_post( $post_id );
// Do something
}
wp_reset_postdata();