通过自定义元和当前日期查询帖子 [英] Query Posts by Custom Meta and Current Date

查看:98
本文介绍了通过自定义元和当前日期查询帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a custom post type called "Events" and I am using Advanced Custom Fields (Wordpress plugin) to add custom fields to the posts. One custom field is the date of the event and my goal is to query the posts based on this date (which is stored in the database as "yymmdd") and only show future events. I've gotten close, but can't seem to figure out how to integrate the filter with the code I've already written.

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

I know that the Wordpress Codex has information on this here (http://codex.wordpress.org/Class_Reference/WP_Query) but being a novice with PHP, I'm at a loss as to how to make it work with my custom field data. Here's the Wordpress code:

// 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' );

但我不确定如何将此与我当前的查询集成或如何使用我的日期字段作为查询值而不是发布日期。以下是我迄今写过的内容。

But I'm not sure how to integrate this with my current query or how to use my date field as the query value rather than the post date. Below is what I've written so far.

<?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; ?>

如果有人能告诉我如何将过滤器添加到我的查询以及如何使用事件日期的自定义字段meta,而不是发布日期。我想我已经提供了所有需要的信息,但是如果我错过了一些东西,请告诉我。感谢!

I would really appreciate if someone could show me how to add the filter to my query and how to use the custom field meta for the date of the event rather than the post date. I think I've given all the information needed but let me know if I missed something. Thanks!

推荐答案

$ b $根据您的需求寻找真正符合要求的meta_query订单b

While researching about the meta_query order by find something that really meets as per your needs

<?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; ?> 

这篇关于通过自定义元和当前日期查询帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆