Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take user directly to the page where event date matches the current date (pagination)

So i have the below function to get my posts, but i want user to go directly on a page where posts have date "today" thats not a event publish date but an date for when event is happening , like if i have events that have man_date field 2015-4-12 and today is 2015-4-12 i will see directly the page where that event is without taking those events on first page.

function get_table(){
                global $wpdb;
                $c_cid = $_REQUEST['cat'];
                if(isset($_REQUEST["page"])){
                    $page_number = filter_var($_REQUEST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number
                    if(!is_numeric($page_number)){die('Invalid page number!');} //incase of invalid page number
                }else{
                    $page_number = 1; //if there's no page number, set it to 1
                }
                $item_per_page = 20;
                //get total number of records from database for pagination
                $get_total_row = $wpdb->get_results("SELECT wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) WHERE wp_term_taxonomy.taxonomy = 'tipo_manifestazione' AND wp_term_taxonomy.term_id = $c_cid AND wp_posts.post_status = 'publish' AND (wp_posts.vip = 'NULL' OR wp_posts.vip IS NULL) AND wp_posts.post_type = 'manifestazione' GROUP BY wp_posts.ID ORDER BY wp_posts.ID DESC");
                $get_total_rows = count($get_total_row);
                //break records into pages
                $total_pages = ceil($get_total_rows/$item_per_page);

                //get starting position to fetch the records
                $page_position = (($page_number-1) * $item_per_page);

                ?>
            <section class="table_container">
                                            <div class="controls_container">
                                              <div class="control">
                                                <select id="months" name="months">
                                                  <option value="" selected>Seleziona Mese</option>
                                                  <option value="01">Gennaio</option>
                                                  <option value="02">Febbraio</option>
                                                  <option value="03">Marzo</option>
                                                  <option value="04">Aprile</option>
                                                  <option value="05">Maggio</option>
                                                  <option value="06">Giugno</option>
                                                  <option value="07">Luglio</option>
                                                  <option value="08">Agosto</option>
                                                  <option value="09">Settembre</option>
                                                  <option value="10">Ottobre</option>
                                                  <option value="11">Novembre</option>
                                                  <option value="12">Dicembre</option>
                                                </select>
                                                </div>
                                                <div class="control">
                                                  <input type="text" name="location_search" id="location_search" placeholder="Luogo..."/>
                                                </div>
                                                <div class="control">
                                                  <div class="loading" style="display:none;"></div>
                                                </div>
                                                <div class="control right">
                                                <?php echo paginate_function($item_per_page, $page_number, $get_total_rows, $total_pages); ?>
                                            </div>
                                            </div>
                                            <header class="table_header">
                                              <div class="sec_th first">
                                                <span class="sec_th_t">Voto</span>
                                              </div>
                                              <div class="sec_th second">
                                                <span class="sec_th_t">Data</span>
                                              </div>
                                              <div class="sec_th third">
                                                <span class="sec_th_t">Manifestazione</span>
                                              </div>
                                              <div class="sec_th fourth">
                                                <span class="sec_th_t">Località</span>
                                              </div>
                                              <div class="sec_th fifth">
                                                <span class="sec_th_t">Percorso Lungo</span>
                                              </div>
                                              <div class="sec_th sixth">
                                                <span class="sec_th_t">Info</span>
                                              </div>
                                              </header>
                                              <div id="rows_container">
                                              <?php
                                                   $query = "SELECT wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) WHERE wp_term_taxonomy.taxonomy = 'tipo_manifestazione' AND wp_term_taxonomy.term_id = $c_cid AND wp_posts.post_status = 'publish' AND (wp_posts.vip = 'NULL' OR wp_posts.vip IS NULL) AND wp_posts.post_type = 'manifestazione' GROUP BY wp_posts.ID ORDER BY wp_posts.ID DESC LIMIT $page_position, $item_per_page";

                                                      $posts = $wpdb->get_results($query);
                                                      foreach($posts as $post){
                                                        $permalink = get_permalink($post->ID);
                                                        $locations = $wpdb->get_results("SELECT * FROM wp_locations WHERE post_id = $post->ID");
                                                        $l = $locations[0];
                                                  ?>
                                              <article id="row_<?php echo $post->ID;?>" class="table_row">
                                                <div class="ar_td first">
                                                  <span class="ar_td_t"><?php echo do_shortcode('[ratings id="'.$post->ID.'"]');?></span>
                                                </div>
                                                <div class="ar_td second">
                                                  <span class="ar_td_t"><?php echo date("d-m-Y", strtotime(get_field('m_f_data',$post->ID)));?></span>
                                                </div>
                                                <div class="ar_td third">
                                                  <a href="<?php echo $permalink;?>"><span class="ar_td_t"><?php echo $post->post_title;?></span></a>
                                                </div>
                                                <div class="ar_td fourth">
                                                 <a href="<?php echo site_url();?>/geo/?location=<?php echo $l->luogo;?>"> <span class="ar_td_t"><?php echo $l->luogo;?></span></a>
                                                </div>
                                                <div class="ar_td fifth">
                                                  <span class="ar_td_t"><?php echo get_field('m_f_percorso_lungo',$post->ID);?></span>
                                                </div>
                                                <div class="ar_td sixth">
                                                  <a href="javascript:void(0);" onclick="show_info('div_<?php echo $post->ID;?>','row_<?php echo $post->ID;?>');" class="show_button">Info +</a>
                                                </div>
                                              </article>
                                              <div id="div_<?php echo $post->ID;?>" class="div_content">
                                                <?php get_post_fields($post->ID);?>
                                              </div>
                                                  <?php
                                                  }
                                                ?>
                                              </div>
                                          </section>
                <?php
                exit();
            }

function paginate_function($item_per_page, $current_page, $total_records, $total_pages)
    {
        $pagination = '';
        if($total_pages > 0 && $total_pages != 1 && $current_page <= $total_pages){ //verify total pages and current page number
            $pagination .= '<ul class="pagination">';

            $right_links    = $current_page + 3; 
            $previous       = $current_page - 3; //previous link 
            $next           = $current_page + 1; //next link
            $first_link     = true; //boolean var to decide our first link

            if($current_page > 1){
                $previous_link = ($previous==0)?1:$previous;
                $pagination .= '<li class="first"><a href="#" data-page="1" title="First">&laquo;</a></li>'; //first link
                $pagination .= '<li><a href="#" data-page="'.$previous_link.'" title="Previous">&lt;</a></li>'; //previous link
                    for($i = ($current_page-2); $i < $current_page; $i++){ //Create left-hand side links
                        if($i > 0){
                            $pagination .= '<li><a href="#" data-page="'.$i.'" title="Page'.$i.'">'.$i.'</a></li>';
                        }
                    }   
                $first_link = false; //set first link to false
            }

            if($first_link){ //if current active page is first link
                $pagination .= '<li class="first active">'.$current_page.'</li>';
            }elseif($current_page == $total_pages){ //if it's the last active link
                $pagination .= '<li class="last active">'.$current_page.'</li>';
            }else{ //regular current link
                $pagination .= '<li class="active">'.$current_page.'</li>';
            }

            for($i = $current_page+1; $i < $right_links ; $i++){ //create right-hand side links
                if($i<=$total_pages){
                    $pagination .= '<li><a href="#" data-page="'.$i.'" title="Page '.$i.'">'.$i.'</a></li>';
                }
            }
            if($current_page < $total_pages){ 
                    $next_link = ($i > $total_pages)? $total_pages : $i;
                    $pagination .= '<li><a href="#" data-page="'.$next_link.'" title="Next">&gt;</a></li>'; //next link
                    $pagination .= '<li class="last"><a href="#" data-page="'.$total_pages.'" title="Last">&raquo;</a></li>'; //last link
            }

            $pagination .= '</ul>'; 
        }
        return $pagination; //return pagination links
    }

ajax:

jQuery("#get_table").load(ajaxurl,{'action':'get_table','cat':cat,'page':page});

The code is working good , to get posts and also pagination working good , the only thing i'm unable to figure out , that how i can make this thing happen to take use directly on page where posts with current matching date are.

like image 500
Arsh Singh Avatar asked May 22 '15 13:05

Arsh Singh


1 Answers

I figured this out by my self.

By using the below query i get the posts with current date and the dates that are passed.

$date = date("Y-m-d");
$posts_c = $wpdb->get_results("SELECT * FROM wp_posts WHERE wp_posts.man_date <= $date");

with above code i will get the rows and now let's convert them to a count

$get_count = count($posts_c);

Now what i did was divide the number of posts by number of posts per page which is 20

 $page_on = ceil($get_count / 20);

Thats it $page_on object contains the value of page where we will send our user.

like image 182
Arsh Singh Avatar answered Oct 19 '22 01:10

Arsh Singh