Advertisement
Guest User

Search.php

a guest
Jul 31st, 2018
1,682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. <?php get_header(); ?>
  2.  
  3. <div id="main-content">
  4. <div class="container">
  5. <div id="content-area" class="clearfix">
  6. <div id="left-area" >
  7. <?php
  8. global $wpdb;
  9. $s = (isset($_GET['s']) && $_GET['s']) ? urldecode($_GET['s']) : false;
  10.  
  11. if($s){
  12. $where = ' title LIKE \'%' . esc_sql( like_escape($s) ) . '%\'';
  13.  
  14. $arr_title = explode(' ',$s);
  15. foreach($arr_title as $search_term):
  16. $where .= ' OR title LIKE \'%' . esc_sql( like_escape( $search_term ) ) . '%\'';
  17. endforeach;
  18.  
  19.  
  20. $query = "
  21. SELECT *
  22. FROM ( SELECT p.ID as id, p.post_title as title, p.post_name as slug, p.post_excerpt as excerpt, 'post' AS type FROM $wpdb->posts AS p WHERE p.post_type IN ('post', 'page') AND p.post_status = 'publish'
  23. UNION ALL
  24. SELECT t.term_id as id, t.name as title, t.slug as slug, tx.description as excerpt, 'term' AS type FROM $wpdb->terms AS t LEFT JOIN $wpdb->term_taxonomy AS tx ON t.term_id = tx.term_id WHERE tx.taxonomy NOT IN ('product_tag', 'post_tag')
  25. ) AS tb WHERE $where ";
  26.  
  27.  
  28. //start pagination
  29. $results = $wpdb->get_results($query);
  30. $total = count($results);
  31. $paged = (isset($_GET['paged'])) ? $_GET['paged'] : 1;
  32. $limit = 50;
  33. //end pagination
  34.  
  35. $offset = $paged ? ($paged - 1) * $limit : 0;
  36. $results = $wpdb->get_results( $query . " LIMIT $offset, $limit ");
  37.  
  38.  
  39. if ($results) :
  40. foreach( $results as $row ) :
  41. $url_img = false; $permalink = false;
  42. if($row->type=='post'){
  43. $url_img = wp_get_attachment_url( get_post_thumbnail_id($row->id) );
  44. $permalink = get_permalink($row->id);
  45. }
  46. if($row->type=='term'){
  47. $permalink = get_bloginfo('url').'/products/'.$row->slug;
  48. }
  49.  
  50. ?>
  51.  
  52. <article id="post-<?php echo $row->id; ?>" class="et_pb_post post-<?php echo $row->id; ?>">
  53.  
  54. <?php if($url_img): ?>
  55. <a href="<?php echo $permalink; ?>"><img src="<?php echo $url_img; ?>" alt="<?php echo $row->title; ?>" /> </a>
  56. <?php endif; ?>
  57.  
  58. <h2 class="entry-title"><a href="<?php echo (!is_wp_error($permalink)) ? $permalink : '#'; ?>"><?php echo $row->title; ?></a></h2>
  59. <?php echo $row->excerpt; ?>
  60. <div class="read-more-link-container"><a href="<?php echo (!is_wp_error($permalink)) ? $permalink : '#'; ?>" class="read-more-link"> Read More</a></div>
  61. </article>
  62.  
  63.  
  64.  
  65. <?php endforeach;
  66.  
  67. if($total > $limit):
  68. $pl_args = array(
  69. 'base' => add_query_arg('paged','%#%'),
  70. 'format' => '',
  71. 'total' => ceil($total / $limit),
  72. 'current' => max(1, $paged),
  73. );
  74. echo paginate_links($pl_args);
  75. endif;
  76.  
  77. else : get_template_part( 'includes/no-results', 'index' );
  78. endif;
  79. }else{
  80. get_template_part( 'includes/no-results', 'index' );
  81. }
  82. ?>
  83. </div> <!-- #left-area -->
  84.  
  85. <?php get_sidebar('search'); ?>
  86. </div> <!-- #content-area -->
  87. </div> <!-- .container -->
  88. </div> <!-- #main-content -->
  89.  
  90. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement