I want to show recent 12 days post then totally random post showing i have done my almost done my work but my problem is i want to show only 12 post in a page then pagination will start. In my code problem will comes for next page if recent post count is more then 12 like 15 then remaining 3 post will showing in next page then random . Please see below my all code.
$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;
}
}
can we resolve from one query.i have used 2 queries and it will create problem. Please check.