我正在尝试在搜索结果中粘贴一篇文章,查找值和meta\\u key meta\\u值。如果帖子中的搜索结果是meta\\u key-sticky和meta\\u value-fatured,则会显示在这些帖子的顶部。这是我的搜索。php:
<?php get_header(); ?>
<?php if (is_paged()) $is_paged = true; ?>
<div class="wrapper" >
<div class="clearfix container_border">
</div>
<?php
$is_search = 0;
global $wpdb;
$totalpost_count = 0;
$all_pids = $wpdb->get_var("SELECT group_concat(ID) FROM $wpdb->posts where post_status=\'publish\'");
$all_pids_arr = explode(\',\',$all_pids);
if($_REQUEST[\'srch_location\'])
{
$is_search = 1;
$srch_location = $_REQUEST[\'srch_location\'];
//$location_pids = $wpdb->get_var("SELECT group_concat(tr.object_id) FROM $wpdb->term_taxonomy tt join $wpdb->term_relationships tr on tr.term_taxonomy_id=tt.term_taxonomy_id where tt.term_id=\\"$srch_location\\"");
$location_pids = $wpdb->get_var("select group_concat(post_id) from $wpdb->postmeta where meta_key like \'add_location\' and meta_value like \\"$srch_location\\"");
$location_pids_arr = explode(\',\',$location_pids);
$all_pids_arr = array_intersect($all_pids_arr,$location_pids_arr);
}
if($is_search && !$all_pids_arr)
{
$all_pids_arr[0] = \'nopost\';
}
if($_REQUEST[\'srch_property_id\'])
{
$post_ids_str = $_REQUEST[\'srch_property_id\'];
$sub_cat_sql .= " and p.ID in ($post_ids_str) ";
}else
{
if($all_pids_arr)
{
$post_ids_str = implode(\',\',$all_pids_arr);
if($post_ids_str)
{
$sub_cat_sql .= " and p.ID in ($post_ids_str) ";
}
}
}
$featurecat = get_cat_id_from_name(get_option(\'ptthemes_featuredcategory\'));
if($featurecat)
{
$srch_feature_pids = $wpdb->get_var("SELECT group_concat(tr.object_id) FROM $wpdb->term_taxonomy tt join $wpdb->term_relationships tr on tr.term_taxonomy_id=tt.term_taxonomy_id where tt.term_id in ($featurecat)");
$srch_feature_pids = \'\';
}
$blogcat = get_cat_id_from_name(get_option(\'ptthemes_blogcategory\'));
$blogcatcatids = get_sub_categories($blogcat,\'string\');
if($blogcatcatids)
{
$srch_blog_pids = $wpdb->get_var("SELECT group_concat(tr.object_id) FROM $wpdb->term_taxonomy tt join $wpdb->term_relationships tr on tr.term_taxonomy_id=tt.term_taxonomy_id where tt.term_id in ($blogcatcatids)");
}
if($srch_blog_pids && $srch_feature_pids)
{
$sub_cat_sql .= " and p.ID not in ($srch_blog_pids,$srch_feature_pids) ";
}elseif($srch_blog_pids && $srch_feature_pids==\'\')
{
$sub_cat_sql .= " and p.ID not in ($srch_blog_pids) ";
}elseif($srch_blog_pids==\'\' && $srch_feature_pids)
{
$sub_cat_sql .= " and p.ID not in ($srch_feature_pids) ";
}
$srch_sql = "select p.* from $wpdb->posts p $post_meta_join where p.post_status=\'publish\' and p.post_type=\'post\' $sub_cat_sql";
if($srch_feature_pids)
{
$feature_srch_sql = "select p.* from $wpdb->posts p where p.post_status=\'publish\' and p.post_type=\'post\' and p.ID in ($srch_feature_pids)";
$srch_sql = " select * from (($feature_srch_sql) union ($srch_sql))";
}
$totalpost_count = $wpdb->get_var("select count(p.ID) from $wpdb->posts p $post_meta_join where p.post_status=\'publish\' and p.post_type=\'post\' $sub_cat_sql");
global $posts_per_page,$paged;
if($paged==\'\'){$paged=1;}
$startlimit = $posts_per_page*($paged-1);
$srch_sql .= " order by p.ID desc limit $startlimit , $posts_per_page";
$post_info = $wpdb->get_results($srch_sql);
?>
<div class="contentarea">
<h5>
<?php
if($_REQUEST[\'s\'] == \'viewmore\')
{
_e(LATEST_PROPERTIES_TEXT);
}
elseif(is_category() && $_REQUEST[\'search\']==\'\')
{
echo single_cat_title();
}else
{
//echo __(SEARCH_TEXT). get_search_param();
echo get_search_param();
}
?></h5>
<?php if($post_info) { ?>
<ul class="display ">
<?php
$count=0;
foreach($post_info as $post_info_obj)
{
$count++;
$post = $post_info_obj;
get_property_info_li($post);
if($count%3==0)
{
?>
<li class="blank"></li>
<?php
}
}
?>
</ul>
<?php
}else
{
_e(NO_PROPERTY_AVAILABLE_MSG);
if($_POST[\'search\']==\'search\')
{
echo get_search_param();
}
}
?>
<?php if($post_info) { ?>
<div class="pagination">
<?php if (function_exists(\'wp_pagenavi\')) { ?><?php wp_pagenavi(); ?><?php } ?>
</div>
<?php }?>
最合适的回答,由SO网友:offroff 整理而成
一个简单的黑客。编辑:将特色帖子ID放入一个数组中,并在主循环中检查该数组。
global $post;
$post_IDs = array();
foreach($post_info as $post_info_obj) {
$post_IDs[] = $post_info_obj->ID;
}
$featured_IDs = array();
$query1 = new WP_Query(array(\'meta_key\'=>\'sticky\',\'meta_value\'=>\'featured\',\'post__in\'=>$post_IDs));
while($query1->have_posts()) {
$query1->the_post();
$featured_IDs[] = $post->ID;
get_property_info_li($post);
}
在主循环中要做的第一件事是:如果帖子有特色,请跳过它。
if(in_array($post_info_obj->ID,$featured_IDs)) { continue; }