【问题标题】:Wordpress get posts after certain date SQL Query issueWordpress 在特定日期后获取帖子 SQL 查询问题
【发布时间】:2012-03-06 23:19:36
【问题描述】:

所以我现在有一种获取帖子的方式:

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

【问题讨论】:

    标签: php sql wordpress


    【解决方案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

    【讨论】:

    • 好吧,这看起来不错 - 现在我该怎么办,我之前提到的 $attachments 和不...
    • 您可以在// do stuff with the post here 处输出您的内容。 $the_query-&gt;the_post() 使用setup_postdata() 函数设置全局帖子信息。现在您可以使用the_title() 之类的函数以及底部的相关函数。您可以使用the_ID() 获取帖子 ID
    【解决方案2】:

    您应该使用过滤器来执行此操作。

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

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

    【讨论】:

    • 所以我在我的帖子中添加了一部分,展示了该函数如何生成 HTML...您介意对我进行更深入的了解吗?传奇!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    相关资源
    最近更新 更多