如何将多个复选框值传递给wordpress查询?

时间:2014-03-20 11:44:49

标签: mysql wordpress checkbox

我正在一个有多个复选框的网站上工作,我想根据用户选择的内容显示结果。

大约有16个音乐复选框,8个场地复选框和6个区域单选按钮。用户可以从音乐类别中选中最多3个复选框,最多3个来自场地,1个来自区域。如何完成此wordpress查询,该查询仅显示所选类别的帖子。

例如,如果用户选择House,Techno和Dubstep in Music,Miami作为Area,他们想要搜索活动,音乐节和现场,我怎样才能使用wordpress查询?

    if(!empty($_POST)){
                global $names;
                $names  = $_POST['music'];
                $music = implode(",", $names);
                $echo $music;
      }

这将echo所有选中的复选框作为house,dubstep,Miami,音乐节。现在我想展示一些关于房子,dubstep和音乐节类别以及迈阿密地区的帖子。

我使用的是

 $args = array (
      'category_name' => $music,
      'posts_per_page' => 12,
      'orderby' => 'title',
      'order' => 'ASC',
     );
$posts = query_posts($args);

但这将显示来自house,dubstep的所有帖子,这很好,但它也显示了音乐节的所有帖子,甚至不在内部和dubstep类别。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在查询中查找类别ID并使用category_and:

if(!empty($_POST)){
       $names  = $_POST['music'];
       $catIds=array();
       foreach($names as $name){
            $catIds[]=get_cat_ID($name);
       }

       $args = array (
           'category__and' => $catIDs,
           'posts_per_page' => 12,
           'orderby' => 'title',
           'order' => 'ASC',
       );
       $posts = query_posts($args);
}
相关问题