所以我目前有获取帖子的方式:
<?php
$args2 = array('post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'order' => 'ASC');
?>
<?php $attachments2 = get_posts( $args2 ); ?>
<?php
if ($attachments2) { ?> ...do stuff...
但我需要做的只是收集帖子,如果它们是在2012年3月1日之后发布的......
所以我不知道该怎么做 - 我被告知要做一个SQL语句,但不知道从哪里开始。
你们会怎么做?
编辑:
以下是整个功能:
<?php
$args2 = array('post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'order' => 'ASC');
?>
<?php $attachments2 = get_posts( $args2 ); ?>
<?php
if ($attachments2) { ?>
<h1 style='float:left'>Photos from the session</h1>
<?php foreach ( $attachments2 as $attachment ) { ?>
<?php if( $attachment->ID != $post_thumbnail_id){ ?>
<div style="width:100%; height:auto;">
<div style="float:left; width: auto; height:auto;">
<?php echo wp_get_attachment_image( $attachment->ID, 'full' ); ?>
<div style="width:auto; height:49px; background-color:#FFF; display:block; width:100%; float:left; margin-bottom:15px;"><div class="floatiefooterright"></div></div>
</div>
<?php $desc = apply_filters( 'the_content', $attachment->post_content );
if($desc != ''){ ?>
<div style="width:auto; height:auto; margin:10px; display:block; float:left;">
<hr class="contentseperator" />
<?php if($desc != ''){ ?>
<em><?php echo $desc ?></em>
<?php } ?>
</div>
<?php } ?>
</div>
<?php }; //end of the "if not featured image"?>
<?php }?>
<?php }?>
答案 0 :(得分:1)
正确,要完全按照自己的意愿行事,就不能使用get_posts()。相反,您可以使用WP_Query class。
这是一个简单的例子,可以帮助您入门:
<?php
// Args
$args = array('post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'order' => 'ASC');
// The Query
add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query( $args );
remove_filter( 'posts_where', 'filter_where' );
// The Loop
while ( $the_query->have_posts() ) {
$the_query->the_post();
// do stuff with the post here
}
// Reset Post Data
wp_reset_postdata();
function filter_where($where = '') {
$where .= " AND post_date >= '2012-03-01'";
return $where;
}
有关更多示例,请参阅WP_Query - Time Parameters。
答案 1 :(得分:0)
您应该使用过滤器来执行此操作。
function after_february($where = '') {
global $wpdb;
$where .= $wpdb->prepare(" AND post_date > %s", "2012-03-01");
return $where;
}
add_filter('posts_where', 'after_february');
$my_posts = get_posts(array('suppress_filters' => false));
remove_filter('posts_where', 'last_thirty_days');
请勿忘记在自定义查询后删除过滤器。