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

我想從特定標記中只帶來10條記錄,並且ID小於一個數字。有沒有辦法用get_posts參數來做到這一點?如何在參數數組中指定大於,小於或不等於?如何使用get_posts獲取大於X(ID)的帖子

謝謝...

回答

0

您必須查詢所有這些,和查詢循環檢查中如果ID是不是你選擇的數量更大或更小。

至於我知道查詢本身不能處理這樣的請求。

1

您需要先獲取ID,然後將這些ID添加到wp_query。

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; 
相關問題