0

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.

2
  • 1
    I assume that this can be done simpler, but your question needs some clarification. You want to list only post from the last 12 days, shuffle them randomly and the display them paginated. Right?
    – kraftner
    Commented Feb 18, 2014 at 10:05
  • yes i need recent 12 days then random. if in 12 days query get 18 post then 12 will showing in 1 page and remaining 6 post will showing in 2nd page then start random. I have done but i am facing problem with limit. So please let me know how i resolve this Commented Feb 18, 2014 at 10:11

1 Answer 1

0

If you are trying to alter your main query have a look at query_posts().

You should be able to achive all you need using query parameters without writing custom queries: WP_Query->orderby & WP_Query->date_query.

4
  • How i get recent 12 post and then random from the WP functions. i want 2 things : 1 : Recent 12days. 2 : then random Commented Feb 18, 2014 at 10:24
  • Oh you only want to mix posts inside one page? So get the 12 most recent posts, shuffle and display them. On page 2 get the next 12 most recent posts and display theme shuffled, right?
    – kraftner
    Commented Feb 18, 2014 at 10:32
  • yes Please see my code you will easily understand what i want exactly Commented Feb 18, 2014 at 10:38
  • You should just query 12 posts, order by date and then mix up the result in PHP if you only want to shuffle inside pages. This way you can also rely on the default WordPress API. Please try this, I gave you all the links/info you need. If you post code for that we can help you further. Your current code is more complicated than necessary.
    – kraftner
    Commented Feb 18, 2014 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.