Support » Plugins » Custom Query for Multiple Tags and Custom Field

  • I am working on a real estate blog. Each post has a custom field called Maximum Guests, which is assigned a value between 1-20. The number of guests is also included in each post content and post excerpt in a standard format. So, either the custom field or the content or excerpt could be used to find this data. Each post is tagged with its amenities (i.e. Deck, Fireplace, Pets Allowed, Ocean View, etc).

    When a site visitor searches, they can choose the total number of guests and types of amenities they are looking for (see for example). I need a query that will pull posts with:

    Number of guests (if visitor inputs 4, the query needs to display posts that specify 4,5,6,7…20 guests)
    Multiple amenities selected

    For instance, a visitor wants a property for 6 people that has a deck and fireplace… how can I make this work?


    works to effectively pull the desired amenities. Then I tried to use $pos = strpos($excerpt, $findme);
    in conjunction with get_the_excerpt() to scan the excerpt for the max number of guests. However, if it’s given 6, it ONLY pulls posts with Maximum Guests 6, not 6,7,8,9,10,11…20.

    Any ideas? Thank you for your time!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Use the ‘tag__in’ argument. You will need to get the term ID for your terms so use $term = get_term_by('name', 'SomeTag', 'post_tag'); and $term->term_id will be your id.

    As for the guests think you will need to test for a value greater than or equal to using the >= operator.

    I am trying to do a very similar thing.

    I see on your site you have implemented a search box just like I want to in your sidebar. How did you do this? Did you use a particular widget or plugin?

    I want to have categories in the top drop down, and tags in the larger window below.

    Please post any code, link, or instructions that helped you figure out how to do this.

    I’ve searched and searched without finding a way to do this.

    Thanks for your help.

    MichaelH, Thanks. I’ll try this. I tried using >= in the query_posts previuosly and it didn’t seem to like that, but I’ll give it another whirl.

    Duane, it took me hours to get it. I pulled the mult. tags using something like:
    `$url = “”;
    $finalurl = $urlsearchterms . “&tag=” . $urltags;`

    and tacking on the string that was searched for and putting + signs btw each.

    The custom field search is a complex database query that 1st looks at the wp_postmeta table, then the wp_posts table. Maybe there was an easier way but that’s how I did it.

    I thought I’d document the way I got this to work without much trouble.

    I made use of the plugin “TDO Tags Fixes” which adds functionality for searching with category and tag intersections. You just install, activate it, and it does it’s magic.

    I used the functionality described here in it’s readme file

    So go to one of your category pages. If your using using fancy permalinks then at the end of the url add “?tdo_tag=a_tag“. You must use the tag slug, not the full tag name. You can use multiple tags as above using “,” and “+”.

    * Example using fancy permalinks: “http://your-blog-uri/category/mycategory/?tdo_tag=tag1+tag2” this would get all posts in the category “mycategory” and tagged with both tag1 and tag2.

    I wanted to have a form that would present a drop down with categories and an area below that where all tags were listed with checkboxes.

    The user selects the category they want to search in and selects one or more tags they want to find.

    Making the form was straight forward and is just simple html. I used the ‘Post” method and pointed the action to a php file I made called INeed.php, you could of course use any name you want for the file.

    The INeed.php file receives the category chosen as a simple $_post variable. It receives the tags chosen as an $_post array.

    From these I then build a url that points to the category and includes a call to “?tdo_tag” to pull the posts with those tags.

    Once I have this url I make a header call with it as the location and the page displays all posts in the category chosen that have all the selected tags.

    I was able to easily implement this both on a search page and in a sidebar using the “PHP Custom Widget” that allowed me to place the html form code inside it.

    Here is the code I used in INeed.php.

    $searchcategory = $_POST["searchcategory"];
    $searchtag = $_POST["searchtag"];
    If (empty($searchtag))
      $url = "" . $searchcategory;
      $counter = 1;
      foreach ($searchtag as $value)
        if ($counter == 1)
          $searchtags = $value;
          $counter += 1;
          $searchtags = $searchtags . "+" . $value;
          $counter += 1;
      $url = "" . $searchcategory . "/?tdo_tag=" . $searchtags;
    header("Location: $url");
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Query for Multiple Tags and Custom Field’ is closed to new replies.