搜索基于多个类别WordPress的帖子

搜索基于多个类别WordPress的帖子

本文介绍了搜索基于多个类别WordPress的帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索wordpress,我想搜索多个类别的文本.这是我的代码

I am working on searching in wordpress i want to search text in multiple categories. here is my code

<form method="post" action="<?php bloginfo('home');?>/?ptype=catsearch" >
    <input type="text" name="searchtext" size="40" />
        <h2><?php _e('Categories:'); ?></h2>
        <select name="category[]" multiple='multiple'>
            <option value=""><?php echo esc_attr(__('Please Select Your Choice')); ?></option>
                <?php $categories=  get_categories('show_count=0&orderby=name&echo=0&hierarchical=true&depth=1&taxonomy=category&exclude=1');
                foreach ($categories as $category) {
                $option = "<option value=$category->term_id>";
                $option .= ucfirst($category->cat_name);
                $option .= '</option>';
                echo $option;
            }
            ?>
        </select>
    <input type="submit" id="searchsubmit" value="Search" name="submit"/>
</form>

当此字体在catsearch.php文件上发布时,我将所有categoryids放入数组中并创建url,请参见下面的代码.如果选择了多个类别,则会创建类似http://abcd.com/?cat=3&cat=7&cat=8&s=dasdasDS的网址.在这种情况下,它仅搜索最后一个cat id中的文本.我认为这使猫毛变得凌乱.我需要在任何类别的搜索文字中都显示所有帖子.

when this foms posts on catsearch.php file i takes all categoryids in array and create url see code below. if muliple categories selected it creates url like http://abcd.com/?cat=3&cat=7&cat=8&s=dasdasDS. In this case it search the text only in last cat id. I think it overights the catids. I need if my search text found in any category then all posts should be displayed.

$categoryids = $_POST['category'];

            echo 'count is ---' .count($categoryids);

            if($categoryids)
            {
                foreach($categoryids as $categoryid)
                {
                    $cat.=  'cat='.$categoryid.'&';
                }
                echo $cat = trim($cat, '&');
                echo '<br />';
                 $url .= '?'.$cat;

            }
if($_POST['searchtext'])
            {
                echo 'searchtext---'. $_POST['searchtext'];
                echo '<br />';
                $url .= '&s='.$_POST['searchtext'];
            }
<META http-equiv="refresh" content="1;URL=<?php echo get_bloginfo('url')."$url"; ?>">

推荐答案

当您这样做时

cat=3&cat=7&cat=8

您正在不断重新定义$ _GET ['cat']的值.因此,完全可以理解为最终将是最后一个-8.

You are constantly redefining the value of $_GET['cat']. So it makes perfect sense that it would end up being the last one - 8.

在wordpress中查询多个类别的语法是使用逗号

The syntax for querying multiple categories in wordpress is using a comma

cat=3,7,8

这篇关于搜索基于多个类别WordPress的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 23:19