2012-03-06 35 views
0

后职位所以我必须获得职位的电流方式:WordPress的获取某一日期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) { ?> ...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

你应该使用过滤器来做到这一点。

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

不要忘记在您的自定义查询后删除过滤器。

+0

我有点困惑,因为如何在我正在做的事情中使用它... – 2012-03-06 23:55:56

+0

所以我已经在我的文章中添加了一部分,它显示了该函数如何生成HTML ...您是否介意稍微我对此有更深入的了解?传说! – 2012-03-06 23:56:30

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

+0

好,所以这看起来不错 - 现在我该怎么办我在前面提到的$附件和什么不是... – 2012-03-06 23:54:15

+0

我编辑了我的问题:S – 2012-03-06 23:54:26

+0

你可以输出你的内容,张贴在这里。 '$ the_query-> the_post()'使用[setup_postdata()](https://codex.wordpress.org/Function_Reference/setup_postdata)函数设置全局发布信息。现在,您可以使用[the_title()](https://codex.wordpress.org/Function_Reference/the_title)等功能以及底部的相关功能。您可以使用[the_ID()](https://codex.wordpress.org/Function_Reference/the_ID)获取帖子ID – drew010 2012-03-07 02:48:43