2012-05-31 2 views
3
$args = array('numberposts' => 10, 'tag' => 'my-tag', 'ID' => 555'); 
$posts = get_posts($args); 

특정 태그에서 10 개의 레코드 만 가져오고 그 ID는 숫자보다 작아야합니다. get_posts 인수로이 작업을 수행 할 수있는 방법이 있습니까? arguments 배열에서 Greater Than, Less Than, Not Like를 어떻게 지정합니까?get_posts를 사용하여 X (ID)보다 큰 게시물을 얻는 방법

감사합니다 ...

답변

0

당신은 그들 모두를 조회해야하고, 쿼리 루프 체크 내부 ID가 선택의 수보다 크거나 작은 경우.

쿼리 자체가 이러한 요청을 처리 할 수 ​​없다는 것을 알고 있습니다.

1

먼저 ID를 가져온 다음 wp_query에 ID를 추가해야합니다.

global $wpdb; 

$post_ids = []; 

// Get all the IDs you want to choose from 
$sql = $wpdb->prepare(
    " 
     SELECT ID 
     FROM $wpdb->posts 
     WHERE ID > %d 
    ", 555); 

$results = $wpdb->get_results($sql); 

// Convert the IDs from row objects to an array of IDs 
foreach ($results as $row) { 
    array_push($post_ids, $row->ID); 
} 

// Set up your query 
$custom_args = array(
    'posts_per_page' => 10, 
    'tag' => 'my-tag', 
    'post__in' => $post_ids 
    ); 

// Do the query 
$custom_query = new WP_Query($custom_args); 

// the loop 
if($custom_query->have_posts()) : 
    while($custom_query->have_posts()) : $custom_query->the_post(); 
     echo get_the_title() . '<br>'; 
    endwhile; 
endif; 
관련 문제