按自定义元和当前日期查询帖子

jmp*_*jmp 5 html php wordpress

我创建了一个名为"Events"的自定义帖子类型,我使用高级自定义字段(Wordpress插件)为帖子添加自定义字段.一个自定义字段是事件的日期,我的目标是根据此日期(在数据库中存储为"yymmdd")查询帖子,并仅显示未来事件.我已经接近了,但似乎无法弄清楚如何将过滤器与我已编写的代码集成.

我知道Wordpress Codex在这里有关于此的信息(http://codex.wordpress.org/Class_Reference/WP_Query)但是作为PHP的新手,我不知道如何使它与我的自定义字段一起工作数据.这是Wordpress代码:

// 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' );
Run Code Online (Sandbox Code Playgroud)

但我不确定如何将其与我当前的查询集成或如何使用我的日期字段作为查询值而不是发布日期.以下是我到目前为止所写的内容.

<?php
$args = array(
            'post_type'         => 'events',
            'posts_per_page'    => 4,
            'meta_key'          => 'event_date', 
            'orderby'           => 'meta_value_num',
            'order'             => 'ASC'
        );
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
?>

    <h1><?php the_title()</h1>
    <?php $eventdate = date_create(get_field('event_date')); ?>
    <small><?php echo date_format($eventdate,'M d'); ?></small>

<?php endwhile; endif; ?>
Run Code Online (Sandbox Code Playgroud)

如果有人能告诉我如何将过滤器添加到我的查询以及如何使用自定义字段元来表示事件的日期而不是发布日期,我将非常感激.我想我已经提供了所需的所有信息,但如果我错过了什么,请告诉我.谢谢!

M K*_*aid 8

在研究meta_query命令时,根据您的需要找到真正符合的东西

<?php
$today = date("Y-m-d");
query_posts(array(
'post_type' => 'events',
'posts_per_page' => 4,
'meta_key' => 'event_date',
'orderby' => 'meta_value',
'order' => 'ASC',
    'meta_query' => array(
        array(
           'key' => 'event_date',
           'meta-value' => $value,
           'value' => $today,
           'compare' => '>=',
           'type' => 'CHAR'// you can change it to datetime also
       )
)
));
if (have_posts()) :
while (have_posts()) : the_post();
?>
    <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>

<?php endwhile; ?>
<?php else : ?>
<?php endif; ?> 
Run Code Online (Sandbox Code Playgroud)