`

Wordpress中自定义字段 custom fields 多字段 查询

阅读更多

Wordpress中实现多个自定义字段 custom fields 查询 例如下图

 

step1


插入字段wordpress会自动加入 wp_postmeta表中去

注意:最好想好要加那些字段 再添加 wordpress不支持 通过界面删除自定义字段

必须要通过SQL语句删除

 

 

 

Step2

在TwentyEleven 主题的基础上的index.php文件中 添加上如下代码

<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage Twenty_Eleven
 */

get_header(); ?>
<script language="JavaScript" type="text/JavaScript">

function showorhidediv(){
var sizeRadio=document.getElementById("SizeRadio");

var vehicleRadio=document.getElementById("VehicleRadio");

var size=document.getElementById("SizeDiv");

var vehicle=document.getElementById("VehicleDiv");
if(sizeRadio.checked)
{
   vehicle.style.display='none';

    size.style.display='block';
}
else if(vehicleRadio.checked)
{
   size.style.display='none';

    vehicle.style.display='block';
}
}
</script>

		<div id="primary">
			<div id="content" role="main">
			
			<form method="get" action="">
			   Search Tyres By 
			    <input type="radio" name="category" value="Vehicle" id="VehicleRadio" onClick="showorhidediv()" checked/>
			   <label for="VehicleRadio">Vehicle</label> 
               
			   <input type="radio" name="category" value="Size" id="SizeRadio"  onClick="showorhidediv()"/>
			   <label for="SizeRadio">Size</label>
			   <div id="SizeDiv" style="width:300px; height:150px;border:1px red solid;">
			      <select name="car" id="car" size="1">                  
				 <option value='' selected="true">Car</option>
				 <option value='CR_V'>CR_V</option>
				 <option value='BMW'>BMW</option>
				 <option value='Truck'>Truck</option>
				 <option value='BENZ'>BENZ</option>
				 <option value='QQ'>QQ</option>
			      </select>
			      <br/>
			      <select name="tread" id="tread" size="1">                  
				 <option value='' selected="true">Tread Width</option>
				 <option value='130'>130</option>
				 <option value='140'>140</option>
				 <option value='150'>150</option>
				 <option value='450'>450</option>
				 <option value='550'>550</option>
				 <option value='650'>650</option>
				 <option value='750'>750</option>
			      </select>
			      <br/>
			      <select name="sidewallheight" id="sidewallheight" size="1">                  
				 <option value='' selected="true">SideWall Height</option>
				 <option value='130'>130</option>
				 <option value='140'>140</option>
				 <option value='150'>150</option>
				 <option value='450'>450</option>
				 <option value='550'>550</option>
				 <option value='650'>650</option>
				 <option value='750'>750</option>
			      </select>
			      <br/>
			      <select name="rim" id="rim" size="1">                  
				 <option value='' selected="true">Rim Diameter</option>
				 <option value='130'>130</option>
				 <option value='140'>140</option>
				 <option value='150'>150</option>
				 <option value='450'>450</option>
				 <option value='550'>550</option>
				 <option value='650'>650</option>
				 <option value='750'>750</option>
			      </select>		
							
			</div>
			
			   <div id="VehicleDiv" style="width:300px; height:150px;border:1px red solid; display:none;" onClick="showorhidediv()">
			      vehicle 
			      vehicle
			      vehicle
							
			</div>
			
				 <p>
					<input type="submit" value="SEARCH" />
				</p>
			
			</form>			
			
			<?php if ( ($_GET['rim'] == '') && ($_GET['tread'] == '') && ($_GET['car'] == '') &&  ($_GET['sidewallheight'] == '')) { ?>
			
			<?php if ( have_posts() ) : ?>

				<?php twentyeleven_content_nav( 'nav-above' ); ?>
			
				<?php /* Start the Loop */ ?>
				<?php while ( have_posts() ) : the_post(); ?>

					<?php get_template_part( 'content', get_post_format() ); ?>

				<?php endwhile; ?>

				<?php twentyeleven_content_nav( 'nav-below' ); ?>

			<?php else : ?>

				<article id="post-0" class="post no-results not-found">
					<header class="entry-header">
						<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentyeleven' ); ?></h1>
					</header><!-- .entry-header -->

					<div class="entry-content">
						<p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'twentyeleven' ); ?></p>
						<?php get_search_form(); ?>
					</div><!-- .entry-content -->
				</article><!-- #post-0 -->

			<?php endif; ?>
			
			<?php } else { ?>
			
			<?php
			$searched_posts = get_reviews_by_custom_search();
			echo $_GET['car'];
			
				foreach ($searched_posts as $searched_post) {
				
						echo "<h1 class=\"entry-title\"><a href=\"" . get_permalink($searched_post->ID) . "\">" . $searched_post->post_title . "</a></h1>";
				
						echo "RimDiameter - " . get_post_meta($searched_post->ID,'rim',true) . "<br>";
						echo "TreadWidth - " . get_post_meta($searched_post->ID,'tread',true) . "<br>";
						echo "Car Make - " . get_post_meta($searched_post->ID,'car',true) . "<br>";	
						echo "SideWallHeight - " . get_post_meta($searched_post->ID,'sidewallheight',true) . "<br>";	
						
						echo "<a href=\"" . get_permalink($searched_post->ID) . "\">Read More</a>";
				
				}	
			}
			?>			

			</div><!-- #content -->
		</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>


 Step3 在functions.php中添加如下代码

 

function get_reviews_by_custom_search() {
global $wpdb;
$rim = $_GET['rim'];
$tread = $_GET['tread'];
$car = $_GET['car'];
$sidewallheight = $_GET['sidewallheight'];

	if ($rim != '') {
		$rim_sql = " AND wpostmeta.meta_key = 'rim' AND wpostmeta.meta_value = '$rim' ";
	}
	if ($tread != '') {
		$tread_sql = " AND wpostmeta2.meta_key = 'tread' AND wpostmeta2.meta_value = '$tread' ";
	}
	if ($car != '') {
		$car_sql = " AND wpostmeta3.meta_key = 'car' AND wpostmeta3.meta_value like '$car' ";
	}	

	if ($sidewallheight != '') {
		$sidewallheight_sql = " AND wpostmeta4.meta_key = 'sidewallheight' AND wpostmeta4.meta_value = '$sidewallheight' ";
	}	

	$querystr = "
		SELECT DISTINCT wposts.*
		FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, $wpdb->postmeta wpostmeta2, $wpdb->postmeta wpostmeta3,$wpdb->postmeta wpostmeta4
		WHERE wposts.ID = wpostmeta.post_id
		AND wposts.ID = wpostmeta2.post_id
		AND wposts.ID = wpostmeta3.post_id
		AND wposts.ID = wpostmeta4.post_id
		" . $rim_sql . "
		" . $tread_sql . "
		" . $car_sql . "
		" . $sidewallheight_sql . "
		ORDER BY wposts.post_date DESC
	";

	$searched_posts = $wpdb->get_results($querystr);
	return $searched_posts;
}
  • 大小: 24.5 KB
  • 大小: 32.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics