programing

Get_posts는 X일 이내 - Wordpress

sourcejob 2023. 4. 4. 21:14
반응형

Get_posts는 X일 이내 - Wordpress

워드프레스 사이트에서는 다음 get_posts 코드를 사용합니다.

get_posts(
        array (
            'numberposts' => 5,
            'orderby'=>'comment_count',
            'order'=>'DESC',
            'post_type'   => array ( 'post' )
        )

게시물이 10일을 넘지 않도록 필터링하려면 어떻게 해야 합니까?따라서 지난 10일 동안의 게시물만 나열해야 합니다.

3.7부터는 date_module https://developer.wordpress.org/reference/classes/wp_query/ #date-parameters를 사용할 수 있습니다.

예를 들어 다음과 같습니다.

$args = array(
    'posts_per_page' => 5,
    'post_type' => 'post',
    'orderby' => 'comment_count',
    'order' => 'DESC',
    'date_query' => array(
        'after' => date('Y-m-d', strtotime('-10 days')) 
    )
); 
$posts = get_posts($args);

의사 선생님 예제는 잘 될 겁니다.get_posts()는 실제 요구를 하기 위해 백그라운드에서 WP_Query()를 사용합니다.이 경우 수정된 예는 다음과 같습니다.

// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
    // posts in the last 30 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-10 days')) . "'";
    return $where;
}

add_filter( 'posts_where', 'filter_where' );
$query = get_posts(array (
            'numberposts' => 5,
            'orderby'=>'comment_count',
            'order'=>'DESC',
            'post_type'   => array ( 'post' )
         ));
remove_filter( 'posts_where', 'filter_where' );

언급URL : https://stackoverflow.com/questions/16994364/get-posts-no-older-than-x-days-wordpress

반응형