Create an advance search page and result page with postmeta in wordpress
step1
Create a new page in your theme with anyname like(ex - advanced-searchform.php) .
and create a search form in it or paste this code
<form method="get" id="advanced-searchform" role="search" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<h3><?php _e( 'Search Products', 'textdomain' ); ?></h3>
<input type="hidden" name="search" value="advanced">
<br>
<input type="text" value="" placeholder="<?php _e( 'Type the Product Name', 'textdomain' ); ?>" name="productname" class="home_searchbox" />
<select name="sortby" id="sortby" class="themebtn">
<option value="post_title"><?php _e( 'Title', 'textdomain' ); ?></option>
<option value="price"><?php _e( 'Price', 'textdomain' ); ?></option>
<option value="manufacturer"><?php _e( 'Store', 'textdomain' ); ?></option>
<option value="date"><?php _e( 'New', 'textdomain' ); ?></option>
</select>
<select name="orderby" id="orderby" class="themebtn">
<option value="asc">ASC</option>
<option value="desc">DESC</option>
</select><br>
<input type="submit" id="searchsubmit" value="Search" />
</form>
Step 2 -
Then call the form into your template like the following:
<?php get_template_part( 'advanced', 'searchform' ); ?>
Step 3 -
Now your search page is ready and then u need to add a function in your function.php of your theme to get all parameters on searchresult page.
<?php
function wpse_load_custom_search_template(){
if( isset($_REQUEST['search']) == 'advanced' ) {
require('advanced-searchresult.php');
die();
}
}
add_action('init','wpse_load_custom_search_template');
?>
or you can useadd_action('template_include', 'advanced_search_tmpl');
function advanced_search_tmpl( $template ) {
if ( is_search() ) {
$t = locate_template('
advanced-searchresult
.php', false);
if ( ! empty($t) ) $template = $t;
}
return $template;
}
Step 4 -
Now create a new searchresult page in your theme folder (ex -
advanced-searchresult.php)
Step 5 -
if you dont want to use custom query with search result then use this code and customize as per your requirements
<?php
// Get data from URL into variables
$_name = $_GET['name'] != '' ? $_GET['name'] : '';
$_model = $_GET['
sortby'] != '' ? $_GET['
sortby'] : '';
// Start the Query
$v_args = array(
'post_type' => 'vehicle', // your CPT
's' => $_name, // looks into everything with the keyword from your 'name field'
'meta_query' => array(
array(
'key' => 'car_model', // assumed your meta_key is 'car_model'
'value' => $_model,
'compare' => 'LIKE', // finds models that matches 'model' from the select field
),
)
);
$vehicleSearchQuery = new WP_Query( $v_args );
// Open this line to Debug what's query WP has just run
// var_dump($vehicleSearchQuery->request);
// Show the results
if( $vehicleSearchQuery->have_posts() ) :
while( $vehicleSearchQuery->have_posts() ) : $vehicleSearchQuery->the_post();
the_title(); // Assumed your cars' names are stored as a CPT post title
endwhile;
else :
_e( 'Sorry, nothing matched your search criteria', 'textdomain' );
endif;
wp_reset_postdata();
?>
and if you use mysql custom query with search then use this code
<?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 = 'product' 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 ";
}elseif($_model=="manufacturer")
{
$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 = 'product' AND (wplc_posts.post_status = 'publish' OR wplc_posts.post_status = 'private') GROUP BY wplc_posts.ID ORDER BY wplc_postmeta.meta_value $_orderby ";
}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 = 'product' 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 = 'product' 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; }
// ends here
//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;
//echo "<pre>"; echo "paged = $paged"; echo "post_per_page = $post_per_page"; echo "offset $offset"; echo "max_num = $max_num_pages"; echo "pagenew = $pagenew";
//echo "<pre>"; print_r($page);
//paged = 1post_per_page = 10offset 0max_num = 5
$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;
//echo $search_query.$limit_query;
$result = $wpdb->get_results($search_query.$limit_query,OBJECT);// return OBJECT
if($result){
?><div class="contain_all_search_data"> <div class="int_store_container"> <?php
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></div>
<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>
</div>
<?php //get_sidebar(); ?>
</div><!--/wrap-->
<?php get_footer(); ?>
Related Posts
How to create a custom advance search result page in wordpress
Get related post or products based on meta key and values through wordpress function
How to create shortcode and use shortcode in Wordpress
Thanks for give us valuable information If you are Looking for WordPress Support , visit on
ReplyDeleteContact WordPress
WordPress Help
WordPress Contact
Create an advance search page and result page with postmeta in wordpress Is Very Helpful. Thanks for such Information
ReplyDeleteLooking for WordPress Support Visit :
WordPress Support
WordPress Technical Support
WordPress Support Phone Number
Superb Information
ReplyDeleteWP Support
Awesome Information
ReplyDelete24/7 WordPress Support
24/7 WP Support
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete