我想显示最近12天的帖子,然后是完全随机的帖子,显示我已经完成了几乎完成的工作,但我的问题是我想在一个页面中只显示12篇帖子,然后分页就会开始。在我的代码中,问题将出现在下一页,如果最近的帖子数超过12,比如15,那么剩下的3篇帖子将显示在下一页中,然后是随机的。请参阅下面我的所有代码。
$limit = 12;
$total_pages = $wpdb->get_var($select);
$page = mysql_escape_string(get_query_var(\'page\'));
if($page)
{
$start = ($page - 1) * $limit;
print_r($start);
}
else{
$start = 0;
}
if(!empty($searchcountry) || !empty($searchresort) || !empty($searchresortresponse))
{
$query = "SELECT `".$wpdb->prefix."posts`.* FROM `".$wpdb->prefix."posts` JOIN `".$wpdb->prefix."postmeta` ON `".$wpdb->prefix."postmeta`.post_id = `".$wpdb->prefix."posts`.ID $where LIMIT $start, $limit";
$record = $wpdb->get_results($query);
}
else
{
$days = "post_date > \'" . date( \'Y-m-d H:i:s\', strtotime( \'-12 days\' ) ) . "\'";
//echo "SELECT * FROM `".$wpdb->prefix."posts` WHERE `post_type` = \'custompost\' and `post_status` = \'publish\' and $days order By `post_date` DESC LIMIT $start, $limit";
//echo "SELECT * FROM `".$wpdb->prefix."posts` WHERE `post_type` = \'custompost\' and `post_status` = \'publish\' and $notin order By RAND() LIMIT $start, $checklimit";
echo $select_query = "SELECT * FROM `".$wpdb->prefix."posts` WHERE `post_type` = \'custompost\' and `post_status` = \'publish\' and $days order By `post_date` DESC LIMIT $start, $limit";
$record1 = $wpdb->get_results($select_query);
$count = count($record1);
// print_r($count); die();
if($count <= 12)
{
$checklimit = 12-$count;
// print_r($checklimit); die();
$ids=array();
foreach($record1 as $key => $id){$ids[] = $id->ID;}
$notin= "`ID` NOT IN (".implode(\',\', array_map(\'intval\', $ids)).")";
$select = "SELECT * FROM `".$wpdb->prefix."posts` WHERE `post_type` = \'custompost\' and `post_status` = \'publish\' and $notin order By RAND() LIMIT $start, $checklimit";
$record2 = $wpdb->get_results($select);
$record = array_merge($record1, $record2);
}
else {
$record = $record1;
}
}
我们可以从一个查询中解析。我使用了2个查询,这将产生问题。请检查。