$args = array('numberposts' => 10, 'tag' => 'my-tag', 'ID' => 555');
$posts = get_posts($args);
我想從特定標記中只帶來10條記錄,並且ID小於一個數字。有沒有辦法用get_posts參數來做到這一點?如何在參數數組中指定大於,小於或不等於?如何使用get_posts獲取大於X(ID)的帖子
謝謝...
$args = array('numberposts' => 10, 'tag' => 'my-tag', 'ID' => 555');
$posts = get_posts($args);
我想從特定標記中只帶來10條記錄,並且ID小於一個數字。有沒有辦法用get_posts參數來做到這一點?如何在參數數組中指定大於,小於或不等於?如何使用get_posts獲取大於X(ID)的帖子
謝謝...
您必須查詢所有這些,和查詢循環檢查中如果ID是不是你選擇的數量更大或更小。
至於我知道查詢本身不能處理這樣的請求。
您需要先獲取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;
,如果你想比X低的ID來獲得職位而很好的解決方案:
$post_ids = range(1, 555);
$args = array('numberposts' => 10,
'tag' => 'my-tag',
'post__in' => $post_ids');
$posts = get_posts($args);
道具girlieworks這裏:https://wordpress.org/support/topic/wp_query-how-to-get-posts-with-and-id-lower-than?replies=7#post-8203891