如何将多个复选框值传递给WordPress Query?

时间:2014-03-20 作者:user48752

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

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

例如,如果用户选择了House、Techno和Dubstep in Music,Miami作为区域,并且他们想要搜索活动、音乐节和现场场馆,我如何使用wordpress查询来获取这些信息?

if(!empty($_POST)){
            global $names;
            $names  = $_POST[\'music\'];
            $music = implode(",", $names);
            $echo $music;
  }
这将显示所有选中的复选框,如house、dubstep、Miami、music Festival。现在我想展示house caegories、dubstep和music Festival类别以及迈阿密地区的帖子。

我用的是

 $args = array (
      \'category_name\' => $music,
      \'posts_per_page\' => 12,
      \'orderby\' => \'title\',
      \'order\' => \'ASC\',
     );
$posts = query_posts($args);
但这将显示所有来自house,dubstep的帖子,这很好,但它也显示了所有来自音乐节的帖子,这些帖子甚至不属于house和dubstep类别。

这是我的HTML 的代码checkboxes

        <div class="customsearchul">
        <div id="searchArea">
        <ul>
        <li><h4>Area:</h4></li>
        <li>All of London<input type="radio" name="area" value="london" checked="london"/></li>
        <li>North<input type="radio" name="area" value="north" /></li>
        <li>East<input type="radio" name="area" value="east" /> </li>   
        <li>South<input type="radio" name="area" value="south" /></li>
        <li>West<input type="radio" name="area" value="west" /></li>
        <li>Central<input type="radio" name="area" value="central" /></li>
        </ul>  
        </div>
        </div>

        <div class="customsearchul">
        <div id="searchMusic">
        <ul>
        <li><h4>Music:</h4></li>
        <li>House<input type="checkbox" name="music[]" value="house" /></li>
        <li>Techno<input type="checkbox" name="music[]" value="techno" /></li>
        <li>Trance<input type="checkbox" name="music[]" value="trance" /></li>
        <li>Electronica<input type="checkbox" name="music[]" value="electronica" /></li>  
        <li>Drum and Bass<input type="checkbox" name="music[]" value="drum-and-bass" /></li>
        <li>Garage<input type="checkbox" name="music[]" value="garage" /></li>
        <li>Dubstep<input type="checkbox" name="music[]" value="dubstep" /></li>
        <li>Trap<input type="checkbox" name="music[]" value="trap" /></li>
        </ul> 
        </div> 
        </div>

        <div class="customsearchul" style="margin-top: 34px;">
        <div id="searchMusic">
        <ul>
        <li>Hip-Hop<input type="checkbox" id="music" name="music[]" value="hiphop" /></li>
        <li>R\'n\'B<input type="checkbox" name="music[]" value="rnb" /></li>
        <li>Rock<input type="checkbox" name="music[]" value="rock" /></li>
        <li>Indie<input type="checkbox" name="music[]" value="indie" /></li>
        <li>Reggae<input type="checkbox" name="music[]" value="reggae" /></li>
        <li>Retro 80\'s/90\'s<input type="checkbox" name="music[]" value="retro" /></li>
        <li>Party Bangers<input type="checkbox" name="music[]" value="party-bangers" /></li>
        <li>Chart Hits<input type="checkbox" name="music[]" value="chart-hits" /></li>
        </ul>  
        </div>
        </div>

        <div class="customsearchul">
        <div id="searchType">
        <ul>
        <li><h4>Search Type:</h4></li>
        <!--<li>Everything<input type="checkbox" name="occasion[]" value="debauchery" /></li> -->
        <li>Nightclubs<input type="checkbox" name="occasion[]" id="nightclub" value="nightclub" checked="checked" /></li>
        <li>Events & Tickets<input type="checkbox" name="occasion[]" value="club-guide" checked="checked" /> </li>   
        <li>Live Venues<input type="checkbox" name="occasion[]" value="festivals" checked="checked" /></li>
        <li>Artist Interviews<input type="checkbox" name="occasion[]" value="artist" checked="checked" /></li>
        </ul>
        </div>
        </div>
       <!-- <input class="searchbutton" type="submit" value="Search" id="performsearch"/> -->

        </form> 
提前感谢您的帮助。

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

好的,这里有一个尝试,但是我不知道你的类别分类法的确切结构,所以它可能需要一些轻微的调整。我已经更新了表单的HTML,以便它使用用户选择的值,然后有一块PHP代码运行自定义WP_Query 要显示相关帖子(您可以编辑这些帖子以显示您喜欢的方式,目前它只返回无序的帖子列表)

HTML表单

<!-- THE HTML FOR THE FORM -->
<form action="" method="post">
    <div class="customsearchul">
    <div id="searchArea">
    <ul>
        <?php // Get the values from $_POST for area
            if($_POST[\'area\']) { $a=$_POST[\'area\'];} else { $a=\'\';} ?>
        <li><h4>Area:</h4></li>
        <li>All of London<input type="radio" name="area" value="london" <?php if ($a == \'london\' || ! isset($_POST[\'area\']) ) { echo \'checked\'; } ?> /></li>
        <li>North<input type="radio" name="area" value="north" <?php if ($a == \'north\' ) { echo \'checked\'; } ?> /> </li>
        <li>East<input type="radio" name="area" value="east" <?php if ($a == \'east\') { echo \'checked\'; } ?> /> </li>   
        <li>South<input type="radio" name="area" value="south" <?php if ($a == \'south\') { echo \'checked\'; } ?> /></li>
        <li>West<input type="radio" name="area" value="west" <?php if ($a == \'west\') { echo \'checked\'; } ?> /></li>
        <li>Central<input type="radio" name="area" value="central" <?php if ($a == \'central\') { echo \'checked\'; } ?> /></li>
    </ul>  
    </div>
</div>

<div class="customsearchul">
    <div id="searchMusic">
        <ul>
        <?php // Get the values from $_POST for music
            if($_POST[\'music\']) { $m=$_POST[\'music\'];} else { $m[]=\'\';} ?>                      
            <li><h4>Music:</h4></li>
            <li>House<input type="checkbox" name="music[]" value="house" <?php if (in_array(\'house\', $m)) { echo \'checked\'; } ?> /></li>
            <li>Techno<input type="checkbox" name="music[]" value="techno" <?php if (in_array(\'techno\', $m)) { echo \'checked\'; } ?> /></li>
            <li>Trance<input type="checkbox" name="music[]" value="trance" <?php if (in_array(\'trance\', $m)) { echo \'checked\'; } ?> /></li>
            <li>Electronica<input type="checkbox" name="music[]" value="electronica" <?php if (in_array(\'electronica\', $m)) { echo \'checked\'; } ?> /></li>  
            <li>Drum and Bass<input type="checkbox" name="music[]" value="drum-and-bass" <?php if (in_array(\'drum-and-bass\', $m)) { echo \'checked\'; } ?> /></li>
            <li>Garage<input type="checkbox" name="music[]" value="garage" <?php if (in_array(\'garage\', $m)) { echo \'checked\'; } ?> /></li>
            <li>Dubstep<input type="checkbox" name="music[]" value="dubstep" <?php if (in_array(\'dubstep\', $m)) { echo \'checked\'; } ?> /></li>
            <li>Trap<input type="checkbox" name="music[]" value="trap" <?php if (in_array(\'trap\', $m)) { echo \'checked\'; } ?> /></li>
            <li>Hip-Hop<input type="checkbox" id="music" name="music[]" value="hiphop" <?php if (in_array(\'hiphop\', $m)) { echo \'checked\'; } ?> /></li>
            <li>R\'n\'B<input type="checkbox" name="music[]" value="rnb" <?php if (in_array(\'rnb\', $m)) { echo \'checked\'; } ?> /></li>
            <li>Rock<input type="checkbox" name="music[]" value="rock" <?php if (in_array(\'rock\', $m)) { echo \'checked\'; } ?> /></li>
            <li>Indie<input type="checkbox" name="music[]" value="indie" <?php if (in_array(\'indie\', $m)) { echo \'checked\'; } ?> /></li>
            <li>Reggae<input type="checkbox" name="music[]" value="reggae" <?php if (in_array(\'reggae\', $m)) { echo \'checked\'; } ?> /></li>
            <li>Retro 80\'s/90\'s<input type="checkbox" name="music[]" value="retro" <?php if (in_array(\'retro\', $m)) { echo \'checked\'; } ?> /></li>
            <li>Party Bangers<input type="checkbox" name="music[]" value="party-bangers" <?php if (in_array(\'party-bangers\', $m)) { echo \'checked\'; } ?> /></li>
            <li>Chart Hits<input type="checkbox" name="music[]" value="chart-hits" <?php if (in_array(\'chart-hits\', $m)) { echo \'checked\'; } ?> /></li>
        </ul>  
    </div>
</div>

<div class="customsearchul">
    <div id="searchType">
        <ul>
        <?php // Get the values from $_POST for music
            if($_POST[\'occasion\']) { $o=$_POST[\'occasion\'];} else { $o[]=\'\';} ?>                                 
            <li><h4>Search Type:</h4></li>
            <!--<li>Everything<input type="checkbox" name="occasion[]" value="debauchery" /></li> -->
            <li>Nightclubs<input type="checkbox" name="occasion[]" id="nightclub" value="nightclub" <?php if (in_array(\'nightclub\', $o)) { echo \'checked\'; } ?> /></li>
            <li>Events &amp; Tickets<input type="checkbox" name="occasion[]" value="club-guide" <?php if (in_array(\'club-guide\', $o)) { echo \'checked\'; } ?> /> </li>   
            <li>Live Venues<input type="checkbox" name="occasion[]" value="festivals" <?php if (in_array(\'festivals\', $o)) { echo \'checked\'; } ?> /></li>
            <li>Artist Interviews<input type="checkbox" name="occasion[]" value="artist" <?php if (in_array(\'artist\', $o)) { echo \'checked\'; } ?> /></li>
        </ul>
    </div>
</div>
<input class="searchbutton" type="submit" value="Search" id="performsearch"/>
</form> 
PHP代码(WP\\U查询)
<?php
    // Get the values from $_POST
    $a = ($_POST[\'area\']);
    $m = ($_POST[\'music\']);
    $o = ($_POST[\'occasion\']);

    // Change the ID of the area category
    $a_tmp = get_category_by_slug( $a );
    $a_catID[] = $a_tmp->term_id;

    // Get the ID of each music category
    // Do some conditional checks to make sure
    // that we\'re correctly indexing an array
    // and that output is an array for the later
    // use with array_merge()
    if ( is_array($m) ) {
        // If we already have an array it means
        // multiple checkboxes have been selected
        foreach( $m as $m_cat ){
            $m_tmp = get_category_by_slug( $m_cat );
            $m_catIDs[] = $m_tmp->term_id;
        }
    } else {
        // If we don\'t have an array then
        // only one checkbox has been 
        // selected
        $m_tmp = get_category_by_slug( $m );
        $m_catIDs[] = $m_tmp->term_id;
    }

    // Get the ID of each occasion category
    // with the checking as described above
    if ( is_array($o) ) {
        foreach( $o as $o_cat ){
            $o_tmp = get_category_by_slug( $o_cat );
            $o_catIDs[] = $o_tmp->term_id;
        }
    } else {
        $o_tmp = get_category_by_slug( $o );
        $o_catIDs[] = $o_tmp->term_id;
    }

    // Concatenate the arrays
    $cat_IDs = array_merge($a_catID,$m_catIDs,$o_catIDs);

    // Build the WP_Query arguments
    $query_args =
        array (
            \'post_type\' => \'post\',
            \'post_status\' => \'publish\',
            //\'category__and\' => $cat_IDs
            \'tax_query\' => array(
                    array(
                        \'taxonomy\' => \'category\',
                        \'field\' => \'term_id\',
                        \'operator\' => \'AND\',
                        \'terms\' => $cat_IDs
                    ),
                )

        );//end $query_args;

    $the_query = new WP_Query($query_args);

    // The Loop to echo posts
    if ( $the_query->have_posts() ) {
            // if we have posts, echo opening <ul>
            echo \'<ul>\';

        while ( $the_query->have_posts() ) {
            // while we have posts, echo the title in an <li>
            $the_query->the_post();
            echo \'<li>\' . get_the_title() . \'</li>\';
        }
            // echo the closing <ul>
            echo \'</ul>\';
    } else {
        // no posts found
        echo \'Nothing found.\';
    } wp_reset_postdata();      /* Restore original Post Data */

?>
几点提示:

您需要仔细检查分类设置,并确保调用get_category_by_slug 正在传递类别的slug可能会更改从循环输出的HTML,因此它不仅仅是<ul>

结束

相关推荐

使用Get_Categories显示类别的图像,或显示任何子帖子中的图像

我正在使用get\\u categories列出父类别的子类别。我想使用get\\u categories输出将图像添加到子类别。我可以从我正在使用get\\u categories的类别的子类别(即父类别的子类别)的任何帖子中获取特色图像。我不想显示任何其他孙儿信息,只想从每组类别的孩子中获得一张特色图片我当前使用的代码是$args = array(\'child_of\' => 1 ); $categories = get_categories($args); forea