meta post search

时间:2012-11-04 作者:iDev Man

我需要查询post元字段,通过多个复选框进行筛选

(image)

当前,在提交表单时,查询仅适用于第二个参数(人员)。

如何使用两个参数进行筛选?

HTML(表单提交)

<input type="checkbox" id="inlineCheckbox1"  name="category[]" value="Planet">
<input type="checkbox" id="inlineCheckbox2" name="category[]" value="People">
PHP(搜索)
                $category = $_GET[\'category\'];

                $category_planet = $category[0];
                $category_people = $category[1];

                $argsearch      = array();                    
                $argsearch["post_type"] = "projects";
                $argsearch[\'showposts\'] = -1;

                if( $advtext!="" ){
                    $advtxtQuery ="select ID from $wpdb->posts
                    LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id where {$wpdb->posts}.post_status = \'publish\'  ";
                    $where =" (( post_typs LIKE \'projects\') AND ( post_title LIKE \'%".$advtext."%\')) ";
                    $where .=" AND ( $wpdb->postmeta.meta_key = \'_cmb_project_category\' AND $wpdb->postmeta.meta_value LIKE \'%".$advtext."%\')";
                    $mypostids = $wpdb->get_col($advtxtQuery." AND ( ".$where." )" );


                    if(count($mypostids)>0){
                        $argsearch[\'post__in\']=$mypostids;
                    }else{
                        $argsearch["s"] = $advtext;
                    }

                }

                if( $category_planet!="" ){
                    if(!isset($argsearch["meta_query"])){
                        $argsearch["meta_query"] = array();
                    }
                    $argsearch["meta_query"][] = array(
                            \'key\' => \'_cmb_project_category\',
                            \'value\' => $category_planet,
                            \'compare\' => \'LIKE\'
                    );

                }


                if( $category_people!="" ){
                     if(!isset($argsearch["meta_query"])){
                          $argsearch["meta_query"] = array();
                     }
                     $argsearch["meta_query"][] = array(
                               \'key\' => \'_cmb_project_category\',
                               \'value\' => $category_people,
                               \'compare\' => \'LIKE\'
                     );

                }

                global $wp_query, $post,$wpdb;
                $query =  new WP_Query($argsearch);
                while($query->have_posts()):
                    $query->next_post();
                $project_list = $query->post->ID;
?> 

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

您的$advtxtQuery 查询--$where =" (( post_typs LIKE \'projects\')...

对于其余部分,两个查询都在同一个键上搜索,因此使用IN 而不是尝试创建两个单独的数组,如Codex example below:

 $args = array(
   \'post_type\' => \'my_custom_post_type\',
   \'meta_key\' => \'age\',
   \'orderby\' => \'meta_value_num\',
   \'order\' => \'ASC\',
   \'meta_query\' => array(
       array(
           \'key\' => \'age\',
           \'value\' => array(3, 4),
           \'compare\' => \'IN\',
       )
   )
 );
 $query = new WP_Query($args);
你可以把你的$category 原来如此,但我会过滤它的恶意。事实上,您的值是(或似乎是)静态的,因此最安全的做法是:

$category_args = array();
if(!empty($category[0])) $category_args[] = "Planet";
if(!empty($category[1])) $category_args[] = "People";
并将其传递到查询中,如下所示:

if( !empty($category_args) ){
  if(!isset($argsearch["meta_query"])){
      $argsearch["meta_query"] = array();
  }
  $argsearch["meta_query"][] = array(
      \'key\' => \'_cmb_project_category\',
      \'value\' => $category_args,
      \'compare\' => \'IN\'
  );
}

结束

相关推荐

Wordpress custom search url

我正在尝试更改我的自定义搜索url以实现SEO目的。我找到一篇关于如何改变的文章mydomain.com/?s=query 到mydomain.com/search/query. 但是,我更喜欢自定义搜索url,例如mydomain.com/something/query.这是否可以实现?谢谢你的帮助!