Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Custom search in Wordpress with multiple categories

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 297
    Comment on it

    Hello readers, today I guide you "How to create search functionality in Wordpress with multiple categories".

    If you want create this functionality, you have to write some code in functions.php file and some code into your file where you want to display search functionality.

     

    functions.php code:-

    In the below code, I create a custom post type called "property" and added three categories
     

    1) Property Place Category
    
    2) Property House Category
    
    3) Property Price Category

     

    You can use below code for creating custom post type and their categories and also add custom meta box for adding extra information about the property.

    /**
     * Post type: Property
     * Author: Evon Technology
     */
    add_action( 'init', 'sukoon_property_content_init' );
    function sukoon_property_content_init() {
    	$labels = array(
    		'name'               			=> _x( 'Property', '' ),
    		'singular_name'      			=> _x( 'Property', '' ),
    		'menu_name'          			=> _x( 'Property', '' ),
    		'name_admin_bar'     			=> _x( 'Property', '' ),
    		'add_new'            			=> _x( 'Add New', '' ),
    		'add_new_item'       			=> __( 'Add New Property', '' ),
    		'new_item'           			=> __( 'New Property', '' ),
    		'edit_item'          			=> __( 'Edit Property', '' ),
    		'view_item'          			=> __( 'View Property', '' ),
    		'all_items'          			=> __( 'All Property', '' ),
    		'search_items'       			=> __( 'Search Property', '' ),
    		'parent_item_colon'  			=> __( 'Parent Property:', '' ),
    		'not_found'          			=> __( 'No Property found.', '' ),
    		'not_found_in_trash' 			=> __( 'No Property found in Trash.', '' )
    	);
    
    	$args = array(
    		'labels'             			=> $labels,
    		'menu_icon'						=> 'dashicons-media-code',
    		'public'             			=> true,
    		'publicly_queryable' 			=> true,
    		'show_ui'            			=> true,
    		'show_in_menu'       			=> true,
    		'query_var'          			=> true,
    		'rewrite'            			=> array( 'slug' => 'property' ),
    		'capability_type'    			=> 'post',
    		'has_archive'        			=> true,
    		'hierarchical'       			=> false,
    		'menu_position'      			=> 12,
    		'supports'           			=> array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    	);
    
    	register_post_type( 'property', $args );
    }
    
    add_action( 'init', 'sukoon_property_place_category_taxonomy_init', 0 );
    
    function sukoon_property_place_category_taxonomy_init() {
    	
    	$labels = array(
    		'name'							=> _x( 'Property Place Category', 'taxonomy general name' ),
    		'singular_name'     			=> _x( 'Property Place Category', 'taxonomy singular name' ),
    		'search_items'      			=> __( 'Search Property Place Category' ),
    		'all_items'         			=> __( 'All Property Place Category' ),
    		'parent_item'       			=> __( 'Parent Property Place Category' ),
    		'parent_item_colon' 			=> __( 'Parent Property Place Category:' ),
    		'edit_item'         			=> __( 'Edit Property Place Category' ),
    		'update_item'       			=> __( 'Update Property Place Category' ),
    		'add_new_item'      			=> __( 'Add New Property Place Category' ),
    		'new_item_name'     			=> __( 'New Genre Property Place Category' ),
    		'menu_name'         			=> __( 'Property Place Category' ),
    	);
    
    	$args = array(
    		'hierarchical'      			=> true,
    		'labels'            			=> $labels,
    		'show_ui'           			=> true,
    		'show_admin_column' 			=> true,
    		'query_var'         			=> true,
    		'rewrite'           			=> array( 'slug' => 'property-place-category' ),
    	);
    	register_taxonomy( 'property-place-category', array( 'property' ), $args );
    }
    
    
    add_action( 'init', 'sukoon_property_house_category_taxonomy_init', 0 );
    
    function sukoon_property_house_category_taxonomy_init() {
    	
    	$labels = array(
    		'name'							=> _x( 'Property House Category', 'taxonomy general name' ),
    		'singular_name'     			=> _x( 'Property House Category', 'taxonomy singular name' ),
    		'search_items'      			=> __( 'Search Property House Category' ),
    		'all_items'         			=> __( 'All Property House Category' ),
    		'parent_item'       			=> __( 'Parent Property House Category' ),
    		'parent_item_colon' 			=> __( 'Parent Property House Category:' ),
    		'edit_item'         			=> __( 'Edit Property House Category' ),
    		'update_item'       			=> __( 'Update Property House Category' ),
    		'add_new_item'      			=> __( 'Add New Property House Category' ),
    		'new_item_name'     			=> __( 'New Genre Property House Category' ),
    		'menu_name'         			=> __( 'Property House Category' ),
    	);
    
    	$args = array(
    		'hierarchical'      			=> true,
    		'labels'            			=> $labels,
    		'show_ui'           			=> true,
    		'show_admin_column' 			=> true,
    		'query_var'         			=> true,
    		'rewrite'           			=> array( 'slug' => 'property-house-category' ),
    	);
    	register_taxonomy( 'property-house-category', array( 'property' ), $args );
    }
    
    add_action( 'init', 'sukoon_property_price_category_taxonomy_init', 0 );
    
    function sukoon_property_price_category_taxonomy_init() {
    	
    	$labels = array(
    		'name'							=> _x( 'Property Price Category', 'taxonomy general name' ),
    		'singular_name'     			=> _x( 'Property Price Category', 'taxonomy singular name' ),
    		'search_items'      			=> __( 'Search Property Price Category' ),
    		'all_items'         			=> __( 'All Property Price Category' ),
    		'parent_item'       			=> __( 'Parent Property Price Category' ),
    		'parent_item_colon' 			=> __( 'Parent Property Price Category:' ),
    		'edit_item'         			=> __( 'Edit Property Price Category' ),
    		'update_item'       			=> __( 'Update Property Price Category' ),
    		'add_new_item'      			=> __( 'Add New Property Price Category' ),
    		'new_item_name'     			=> __( 'New Genre Property Price Category' ),
    		'menu_name'         			=> __( 'Property Price Category' ),
    	);
    
    	$args = array(
    		'hierarchical'      			=> true,
    		'labels'            			=> $labels,
    		'show_ui'           			=> true,
    		'show_admin_column' 			=> true,
    		'query_var'         			=> true,
    		'rewrite'           			=> array( 'slug' => 'property-price-category' ),
    	);
    	register_taxonomy( 'property-price-category', array( 'property' ), $args );
    }
    
    
    /*
    * Custom Address Box
    */
    
    add_action( 'admin_init', 'price_metabox' );
    function price_metabox()
    {
    	add_meta_box( 'price_meta_box',
    		'Price Metabox',
    		'price_review_meta_box',
    		'property', 'normal', 'high'
    	);    
    }
    
    //Add custom post meta boxes
    function price_review_meta_box( $post ) {    
        $price_caption = esc_html( get_post_meta( $post->ID, 'price_caption', true ) );
        $streat_name = esc_html( get_post_meta( $post->ID, 'streat_name', true ) );
        $place_name = esc_html( get_post_meta( $post->ID, 'place_name', true ) );
        $living_area = esc_html( get_post_meta( $post->ID, 'living_area', true ) );
        $property_type = esc_html( get_post_meta( $post->ID, 'property_type', true ) );
        ?>
         <table style="width:100%;">
     		<tr>
     		    <td style="width: 20%">Caption</td>
     		    <td style="width: 40%">
     		    	<input type="text" size="70" name="price_caption" placeholder="Price" value="<?php echo $price_caption; ?>">
     		    </td>
     		</tr>
     		<tr>
     		    <td style="width: 20%">Streat Number</td>
     		    <td style="width: 40%">
     		    	<input type="text" size="70" name="streat_name" placeholder="Streat Number" value="<?php echo $streat_name; ?>">
     		    </td>
     		</tr>
     		<tr>
     		    <td style="width: 20%">Place Name</td>
     		    <td style="width: 40%">
     		    	<input type="text" size="70" name="place_name" placeholder="Place Name" value="<?php echo $place_name; ?>">
     		    </td>
     		</tr>
     		<tr>
     		    <td style="width: 20%">Living Area</td>
     		    <td style="width: 40%">
     		    	<input type="text" size="70" name="living_area" placeholder="Living Area" value="<?php echo $living_area; ?>">
     		    </td>
     		</tr>
     		<tr>
     		    <td style="width: 20%">Property Type</td>
     		    <td style="width: 40%">
     		    	<input type="text" size="70" name="property_type" placeholder="Property Type" value="<?php echo $property_type; ?>">
     		    </td>
     		</tr>
         </table>
    <?php
    }
    
    //Save custom post meta values
    add_action( 'save_post', 'price_metabox_fields', 10, 2 );
    function price_metabox_fields( $caption_metabox_id )
    {
    	if ( isset( $_POST['price_caption'] ) )	{
    		update_post_meta( $caption_metabox_id, 'price_caption', $_POST['price_caption'] );
    	}
    	if ( isset( $_POST['streat_name'] ) )	{
    		update_post_meta( $caption_metabox_id, 'streat_name', $_POST['streat_name'] );
    	}
    	if ( isset( $_POST['place_name'] ) )	{
    		update_post_meta( $caption_metabox_id, 'place_name', $_POST['place_name'] );
    	}
    	if ( isset( $_POST['living_area'] ) )	{
    		update_post_meta( $caption_metabox_id, 'living_area', $_POST['living_area'] );
    	}
    	if ( isset( $_POST['property_type'] ) )	{
    		update_post_meta( $caption_metabox_id, 'property_type', $_POST['property_type'] );
    	}
    }

     

    Now, see your admin dashboard, Custom Post Type called "Property" is created and their category. You can add a category and create a new post. While creating post assign category to this post.

     

    index.php code:-

    In the below code first, I had created a drop-down box to display " Category ", there are 3 drop-down in my code and a search button. User selects the category and clicks a search button, on the basis of selecting category related post will be shown in their currenviewportrt or their screen.

    You can put the below code into your index.php file or where you want to display search functionality.

     

    <div id="searchpage-form">	
    	<form method="post">
    		<div class="srow">
    			<div class="search1">
    				<select name="place" class="selectBox">
    					<option value="">Select Place</option>
    				<?php
    					$place_taxonomy = 'property-place-category';
    					$places = get_terms( $place_taxonomy, array(
    						'order'  	 => 'ASC',
    						'hide_empty' => false
    					));
    		            $i=1;
    		            foreach ($places as $place) {
    				?>
    					<option value="<?php echo $place->term_id;?>" ><?php echo $place->name;?></option>
    				<?php
    					$i++;
    					}
    				?>
    				</select>
    			</div>
    			<div class="search2">
    				<select name="type">
    					<option value="">Select Type</option>
    				<?php
    					$house_taxonomy = 'property-house-category';
    					$houses = get_terms( $house_taxonomy, array(
    						'order'  	 => 'ASC',
    						'hide_empty' => false
    					));
    		            $j=1;
    		            foreach ($houses as $house) {
    				?>
    				
    					<option value="<?php echo $house->term_id;?>"><?php echo $house->name;?></option>
    					
    				<?php
    					$j++;
    					}
    				?>
    				</select>
    			</div>
    			<div class="search3">	
    				<select name="price">
    					<option value="">Select Price</option>
    				<?php
    					$price_taxonomy = 'property-price-category';
    					$prices = get_terms( $price_taxonomy, array(
    						'order'  	 => 'ASC',
    						'hide_empty' => false
    					));
    		            $k=1;
    		            foreach ($prices as $price) {
    				?>
    					<option value="<?php echo $price->term_id;?>"><?php echo $price->name;?></option>
    					
    				<?php
    					$k++;
    					}
    				?>
    				</select>
    			</div>		
    			<input type="hidden" value="searched" name="searched">
    			<div id="searchpagesubmit">
    				<input type="submit" name="search" id="searchbutton" value="Search" style="" >
    			</div>
    		</div>
    	</form>
    </div>
    <div class="text">
    	<h2>Search Results</h2>
    </div>
    <div id="results"> 
    	<?php
    		if(isset($_POST['search'])) {
    			$place = $_POST['place'];
    			$type = $_POST['type'];
    			$price = $_POST['price'];
    
    			$args = array(
    				'post_type'              => 'property',
    				'post_status'            => 'publish',
    				'posts_per_page'         => -1,
    				'tax_query' => array(
    					'relation' => 'AND',
    					array(
    						'taxonomy' => 'property-place-category','property-house-category','property-price-category',
    			 			'terms'    => array( $place,$type,$price ),
    			 			'operator' => 'IN'
    					),
    				)
    			);
    		} else {
    			$args = array(
    				'post_type'              => 'property',
    				'post_status'            => 'publish',
    				'posts_per_page'         => -1
    			);
    		}
    		wp_reset_query();
    		$i=1;
    		$loop = new WP_Query($args);
    		if( $loop->have_posts() ) {
    		   while( $loop->have_posts()) : $loop->the_post();
    			$featuredImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
    
    			$price_caption 	= esc_html( get_post_meta( $post->ID, 'price_caption', true ) );
    			$streat_name 	= esc_html( get_post_meta( $post->ID, 'streat_name', true ) );
    			$place_name 		= esc_html( get_post_meta( $post->ID, 'place_name', true ) );
    			$living_area 	= esc_html( get_post_meta( $post->ID, 'living_area', true ) );
    			$property_type 	= esc_html( get_post_meta( $post->ID, 'property_type', true ) );
    	?>
    		<hr>
    		<div class="search-result">
    			<div class="result_block">
    				<div class="nieuw-img floatleft">
    					<a href="#">
    						<img src="<?php echo $featuredImg[0];?>" style="opacity: 1;">
    					</a>
    				</div>
    				<div class="search-result-text">
    					<div class="add-result-text">
    						<h2>
    							<a href="#" style="background: rgb(255, 255, 255) none repeat scroll 0% 0%;"><?php the_title();?></a>
    						</h2>
    						<a href="#"><?php echo $streat_name;?></a>, <a href="#"><?php echo $place_name;?></a><br><br>Living Area:
    						<?php echo $living_area;?>
    						<a href=""></a>
    						<div class="highlight"><?php the_content();?></div>
    					</div>
    					<div class="add-result-text2">	
    						<strong><?php echo $property_type;?>  <?php echo $price_caption;?></strong> 
    					</div>
    				</div>
    			</div>
    		</div>
    	<?php
    					$i++;
    			   endwhile;
    		    }
    	?>
    </div>

     

    I hope it helps you.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: