Skip to main content

Show Custom type posts with pagination In WORDPRESS


Show Custom type posts with pagination In WORDPRESS
                                       OR
Custom Query results  with page-navi pagination in Wordpress
                                       OR
Show Custom Posts with meta value by custom query


<?php
/*
Template Name: Search Page
*/
?>

<?php get_header(); ?>


    <div id="wrap">
        <div id="content">
        <div class="clearfix"></div>
       
        <?php
// Get data from URL into variables
$_name = $_GET['productname'] != '' ? $_GET['productname'] : '';
$_model = $_GET['sortby'] != '' ? $_GET['sortby'] : '';
$_orderby = $_GET['orderby'] != '' ? $_GET['orderby'] : '';


global $wpdb;
global $post;
global $wp_query;

// Pagination Setup
/*
$posts_per_page = 20;
$start = 0;
$paged = get_query_var( 'paged') ? get_query_var( 'paged', 1 ) : 1; // Current page number
$start = ($paged-1)*$posts_per_page;
*/

if($_model=="price")
{
$search_query = "SELECT SQL_CALC_FOUND_ROWS wplc_posts.ID FROM wplc_posts INNER JOIN wplc_postmeta ON ( wplc_posts.ID = wplc_postmeta.post_id and wplc_postmeta.meta_key='price' ) INNER JOIN wplc_postmeta AS mt1 ON ( wplc_posts.ID = mt1.post_id and wplc_postmeta.meta_key='price' ) WHERE 1=1 AND (wplc_posts.post_title LIKE '%$_name%' OR wplc_posts.post_content LIKE '%$_name%') OR ( wplc_postmeta.meta_key = 'price' AND ( ( ( mt1.meta_key = 'price' AND CAST(mt1.meta_value AS CHAR) LIKE '%$_name%' ) OR ( mt1.meta_key = 'manufacturer' AND CAST(mt1.meta_value AS CHAR) LIKE '%$_name%' ) ) ) ) AND wplc_posts.post_type = 'post' AND (wplc_posts.post_status = 'publish' OR wplc_posts.post_status = 'private') GROUP BY wplc_posts.ID ORDER BY wplc_postmeta.meta_value+0 $_orderby LIMIT 0, 20 ";
}elseif($_model=="date")
{
$search_query = "SELECT SQL_CALC_FOUND_ROWS wplc_posts.ID FROM wplc_posts INNER JOIN wplc_postmeta ON ( wplc_posts.ID = wplc_postmeta.post_id and wplc_postmeta.meta_key='manufacturer' ) INNER JOIN wplc_postmeta AS mt1 ON ( wplc_posts.ID = mt1.post_id and wplc_postmeta.meta_key='manufacturer' ) WHERE 1=1 AND (wplc_posts.post_title LIKE '%$_name%' OR wplc_posts.post_content LIKE '%$_name%') OR ( wplc_postmeta.meta_key = 'price' AND ( ( ( mt1.meta_key = 'price' AND CAST(mt1.meta_value AS CHAR) LIKE '%$_name%' ) OR ( mt1.meta_key = 'manufacturer' AND CAST(mt1.meta_value AS CHAR) LIKE '%$_name%' ) ) ) ) AND wplc_posts.post_type = 'post' AND (wplc_posts.post_status = 'publish' OR wplc_posts.post_status = 'private') GROUP BY wplc_posts.ID ORDER BY wplc_posts.post_date $_orderby  ";

}else {
$search_query = "SELECT SQL_CALC_FOUND_ROWS wplc_posts.ID FROM wplc_posts INNER JOIN wplc_postmeta ON ( wplc_posts.ID = wplc_postmeta.post_id and wplc_postmeta.meta_key='price' ) INNER JOIN wplc_postmeta AS mt1 ON ( wplc_posts.ID = mt1.post_id and wplc_postmeta.meta_key='price' ) WHERE 1=1 AND (wplc_posts.post_title LIKE '%$_name%' OR wplc_posts.post_content LIKE '%$_name%') OR ( wplc_postmeta.meta_key = 'price' AND ( ( ( mt1.meta_key = 'price' AND CAST(mt1.meta_value AS CHAR) LIKE '%$_name%' ) OR ( mt1.meta_key = 'manufacturer' AND CAST(mt1.meta_value AS CHAR) LIKE '%$_name%' ) ) ) ) AND wplc_posts.post_type = 'post' AND (wplc_posts.post_status = 'publish' OR wplc_posts.post_status = 'private') GROUP BY wplc_posts.ID ORDER BY wplc_posts.post_title $_orderby ";

}



 function get_url_var($name)
{
    $strURL = $_SERVER['REQUEST_URI'];
    $arrVals = split("/",$strURL);
    $found = 0;
    foreach ($arrVals as $index => $value)
    {
        if($value == $name) $found = $index;
    }
    $place = $found + 1;
    return $arrVals[$place];
}

$page = get_url_var('page');
if(is_numeric($page))
{
    $countpage=$page;
}else { $countpage = 1;  }



 //next pagination starts

  $total_record = count($wpdb->get_results($search_query, ARRAY_A));
    $paged      = !empty($countpage) ? $countpage: 1;
    $post_per_page  = get_option('posts_per_page');
    $offset         = ($paged - 1)*$post_per_page;
    $max_num_pages  = ceil($total_record/ $post_per_page);
    $pagenew = get_query_var('page') ? get_query_var('page') : 1;



    $wp_query->found_posts = $total_record;
    // number of pages
    $wp_query->max_num_pages = $max_num_pages;

    $limit_query    =   " LIMIT ".$post_per_page." OFFSET ".$offset;   


    $result =   $wpdb->get_results($search_query.$limit_query,OBJECT);// return OBJECT

   if($result){
    foreach ($result as $rowpost):
      //setup_postdata($post);
                  
           $row_pro_id = $rowpost->ID;
    $row_post_details = get_post($row_pro_id);
    $post_pro_title = $row_post_details->post_title;
    //echo "<pre>"; print_r($row_post_details);
    $meta_values = get_post_meta($row_pro_id, "price", true);
    $meta_manufacturer = get_post_meta($row_pro_id, "manufacturer" , true);
    $meta_image_large= get_post_meta($row_pro_id, "image_large" , true);
   
   
        if (strlen($post_pro_title) > 35) {
         $product_title = substr($post_pro_title, 0, 35);
        $product_title = substr($product_title, 0, strrpos($product_title, ' ')) . " ...";
      } else {
        $product_title = $post_pro_title;
      }

    //echo "<p>$row_pro_id</p>";
    ?>
    <div class="col-sm-3 product_wrapper">
      <div>
         <a href="<?php echo $row_post_details->guid; ?>">
         <?php if (@getimagesize($meta_image_large) === false) { ?>
          <img  class="default-img-class" src="<?php bloginfo('template_directory'); ?>/not-available.jpg" alt="<?php echo $product_title; ?>" height="150px" width="150px" />
          <?php } else { ?>
         <img  class="default-img-class" src="<?php echo $meta_image_large; ?>" alt="<?php echo $product_title; ?>" height="150px" width="150px" /><?php } ?>
         </a>
      </div>
      <div class="title_wrapper">
        <a class="nodecor" href="<?php echo $row_post_details->guid; ?>" rel="bookmark" title="Permanent Link to <?php echo $product_title; ?> "><?php echo $product_title; ?></a>
        <div class="manu_title"><?php if(!empty($meta_manufacturer)) { echo $meta_manufacturer; } ?></div>
      </div>
      <p class="price_wrapper">$ <?php echo $meta_values;  ?></p>
  </div>
          
          
        <?php
        endforeach;
        ?>
       <div class="navigation" style="margin-bottom:20px;"><?php wp_pagenavi(); wp_reset_query(); ?></div>
        <?php
 
 //next pagination ends here


 } else{
?>
 <h3>404 Nothing here. Sorry.</h3>


<?php } ?>
<div class="navigation">
    <div class="previous panel"><?php //previous_posts_link('&laquo; previous ',$max_num_pages)
    ?>
    </div>
    <div class="next panel"><?php //next_posts_link('more  &raquo;',$max_num_pages)
     ?>
    </div>
   
</div>


</div>   
           <?php //get_sidebar(); ?>
    </div><!--/wrap-->
<?php get_footer(); ?>



Comments

Post a Comment

Popular posts from this blog

Run and compile sass scss file to css using node

  Today we learn how to use scss and generate css using node  or Run and compile sass scss file to css using node   So please follow simple  steps :-   Today we will create a project that can read scss file and generates css with it  Note: Make sure you have installed node in your system. If you want to help to install node js based on your system then check our other tutorial or check node js official website. Now create a blank folder and open  terminal(linux) or cmd(windows) and navigate to your current project folder by using cd command Now run below command npm init after enter it will ask you some package info that you can fill according to you or just keep enter until it finished. The above command will generate package.json file Now  we will install npm module that will convert our scss to css Run below command: npm install node-sass So we have installed node-sass package . Now open package.json file in your...

Solution-windows 'expo' is not recognized as an internal or external command

Solution for expo is not recognized as an internal or external command,operable program or batch file in Windows 10 Sometimes expo will not work globally mostly in windows 10, If you are facing same issue then follow the below Steps 1) Click on windows button and search for  " Environment variables"  and click on "Edit the system environment variables" 2) Now you will see a popup like below screen. Then you need to click on Environment Variables. (Please see highlight part in below image)     3)Then click on new button that i have highlighted in below image 4. Then a popup will open and you need to fill details like below mentioned Variable Name :Path Variable Value: %USERPROFILE%\AppData\Roaming\npm Here we are creating a new path variable and passing location of npm.   Now Click on OK and close all the terminal windows and open new CMD or terminal and type Expo . Great now you can access expo from any...

Copy text or html to clipboard js php

How to copy text or HTML to clipboard using JS/jQuery Today we learn how to copy texts and html into the clipboard by using js, we can copy content on button click or any event through JS. Here are the few simple methods for it. Please follow the below methods 1) Copy data-attribute value to clipboard   If you have an html element like a button with data value and you want to copy value on button click then use below function HTML CODE <a class="cls_copy_pg_action copyAction copy-action-btn" data-value="THIS TEXT WILL BE COPIED" > <i class="far fa-copy"></i> Copy</a> JQUERY CODE $(document).on("click", ".copy-action-btn", function() {       var trigger = $(this);       $(".copy-action-btn").removeClass("text-success");       var $tempElement = $("<input>");         $("body").append($tempElement);     ...