我有一个自定义的帖子类型,叫做client
它有一个自定义字段next_due_date
, 那是一个没有时间的日期。我想有一个名单的职位,有一个下一个到期日即将到来。我尝试用以下代码创建一个名为“due soon”的页面模板(包括Tim Malone建议的更改):
$loop = new WP_Query(array(
\'post_type\' => \'client\',
\'posts_per_page\' => -1,
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\',
\'meta_key\' => \'next_due_date\',
\'meta_query\' => array(array(
\'key\' => \'meta_value_num\',
\'value\' => date(\'YY-mm-dd\',strtotime("today")+(7*60*60*24)),
\'compare\' => \'<=\',
\'type\' => \'DATE\'
))
));
while ( $loop->have_posts() ) : $loop->the_post();
responsive_entry_before(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php responsive_entry_top(); ?>
<h2 class="entry-title post-title"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<div class="post-entry">
<?php echo "File # : " . get_post_meta($post->ID,\'file_number\',true) . "<br />"; ?>
<?php echo "File Status : " . get_post_meta($post->ID,\'file_status\', true) . "<br />"; ?>
</div><!-- end of .post-entry -->
<div class="post-meta">
<?php responsive_post_meta_data(); ?>
<?php if ( comments_open() ) : ?>
<span class="comments-link">
<span class="mdash">—</span>
<?php comments_popup_link(
__( \'No Comments ↓\', \'responsive\' ),
__( \'1 Comment ↓\', \'responsive\' ),
__( \'% Comments ↓\', \'responsive\' ) ); ?>
</span>
<?php endif; ?>
</div><!-- end of .post-meta -->
________________________________
<?php responsive_entry_bottom(); ?>
</div><!-- end of #post-<?php the_ID(); ?> -->
<?php responsive_entry_after();
endwhile;
我还不能让它输出任何东西,更不用说弄清楚如何定义即将到来的时间(比如7天)。
下面是我用来计算帖子类型和字段设置为打开的帖子类型的代码:
global $wpdb;
$meta_key = \'file_status\';
$meta_value = \'open\';
$sql = "SELECT count(DISTINCT pm.post_id)
FROM $wpdb->postmeta pm
JOIN $wpdb->posts p ON (p.ID = pm.post_id)
WHERE pm.meta_key = \'$meta_key\'
AND pm.meta_value = \'$meta_value\'
AND p.post_type = \'client\'
AND p.post_status = \'publish\'
";
$count = $wpdb->get_var($sql);
$count_posts = wp_count_posts(\'client\');
$published_posts = $count_posts->publish;
echo "Number of Clients: " . $published_posts;
echo "<p>Open Files: $count</p>";
我还想数一数即将到期的那些
最合适的回答,由SO网友:Tim Malone 整理而成
首先,the_post()
实际上没有输出任何内容-它只是设置准备输出的post数据。您需要这样做才能显示帖子:
<?php
while ( $loop->have_posts() ) :
$loop->the_post();
the_title();
the_content();
endwhile;
?>
您还可以使用其他几个标记,并且您希望围绕这些标记格式化HTML以显示您想要的方式,如果您也需要这方面的指导,请告诉我。
对于查询,您可能会发现这非常有用:http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
您的排序非常接近,但要限制您的帖子在未来7天内返回任何内容,您需要使用meta\\u查询。您可以这样做:
<?php
$loop = new WP_Query(array(
\'post_type\' => \'client\',
\'posts_per_page\' => -1,
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\',
\'meta_key\' => \'next_due_date\',
\'meta_query\' => array(array(
\'relation\'=> \'AND\',
array(
\'key\' => \'next_due_date\',
\'value\' => date(\'Y-m-d\',strtotime("today")+(7*60*60*24)),
\'compare\' => \'<=\',
\'type\' => \'DATE\'
),
array(
\'key\' => \'next_due_date\',
\'value\' => date(\'Y-m-d\',strtotime("today")),
\'compare\' => \'>=\',
\'type\' => \'DATE\'
)
))
));
?>
我还没有测试过这段代码,但我认为它是正确的-让我知道你的进展如何。7天由
7*60*60*24
- 60秒x 60分钟x 24小时x 7天。
UPDATED as per comments to also filter out posts in the past