1

I have a custom post type bananas with a custom field _bananas_size (size is a number). Now I'd like a search field that only searches the custom field size. For example: the visitors selects value 6 and hits search. Then I want only the bananas with a size of 6 or bigger to show up as results. I've found multiple topics on how to handle a search with custom post types. However I haven't found how to limit the search to a custom post field.

What I currently have is this:

searchform.php

<form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
<input type="hidden" name="post_type" value="bananas" /> 
<select name="size">
    <option value="4">4</option>
    <option value="6">6</option>
    <option value="8">8</option>
    <option value="10">10</option>
</select>

<input type="submit" alt="Search" value="Search" />
</form>

search.php

$type = 'bananas';
$args=array(
    'post_type' => $type,
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_key' => '_bananas_size',
    'meta_query' => array(
        array(
            'key' => '_bananas_size',
            'value' => $_GET['size'],
            'compare' => '>=',
            'type' => 'NUMERIC'
        )
    )
);

$my_query = null;
$my_query = new WP_Query($args);

This returns a list of all bananas, no matter the size. How can I limit the search to the _bananas_size field?


Update:

I tried to do a var_dump like Tomás Cot suggested below but I got no output at all (even not from an echo "<h1>Size: </h1>";). I changed the searchform.php to:

<form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">

<input type="search" name="s" placeholder="Search&hellip;">
<input type="hidden" name="post_type" value="bananas">


<input type="submit" alt="Search" value="Search" />
</form>

This resulted in the following output:

var_dump($_GET['size']);: NULL

var_dump($my_query->request);: http://pastebin.com/XYwqW2F9

Update 2:

It turned out that I had a typo in my code (fieldname was _bananassize, not _bananas_size). A very big thanks to @Tomás Cot for finding this! The code he posted below in the answer works great after I fixed my mistake.

  • Banana size should probably be a custom taxonomy, especially if you're wanting to use it as a filter. Also don't discard the search query like that ( you may as well just use query_posts in this case as you're doing the same thing ). Use pre_get_posts to modify the search query before it happens – Tom J Nowell Aug 10 '14 at 23:31
  • @TomJNowell I don't think a custom taxonomy is the way to go for me in this situation since the size of the banana can be any value larger then zero. In my question I only wrote a select for quick testing, in reality it would be an input field. I don't yet completely understand the way I got to go with pre_get_posts, so I'll have to dive into that one. I take it it will be better if I made a widget and use that instead of completely discarding the search query and then use pre_get_posts to manipulate the query somehow so that it filters on _bananas_size – user57948 Aug 11 '14 at 10:55
  • Or you can write code to auto-assign it a term such as 5-8 10-20 etc then use those terms to filter and fill out your drop down, you'd get free taxonomy archives for it too so you wouldn't even need to modify any queries – Tom J Nowell Aug 11 '14 at 11:10
  • I understand the advantage of using an interval like that, however there might once be a situation where it's important to have exact values. For example with machinery that require a specific volume. – user57948 Aug 11 '14 at 14:46
  • can you do a var_dump($_GET); with the original form? – Tomás Cot Aug 11 '14 at 16:37
0
$type = 'bananas';
$args=array(
    'post_type' => 'bananas',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_key' => '_bananas_size',
    'meta_query' => array(
        array(
            'key' => '_bananas_size',
            'value' => $_GET['size'],
            'compare' => '>=',
            'type' => 'NUMERIC'
        )
    )
);

$my_query = null;
$my_query = new WP_Query($args);

You can read more about the parameters for the WP_Query here. Also, some type of data validation and sanitization is recommended.

  • Oops, I wasn't being paying enough attention when I wrote that part. And you're totally correct on data validation, however I'm just playing with this for now and I wanted to keep it as clean as possible. The code you suggest however doesn't return what I want. I think what TomJNowell suggests is the way to go – user57948 Aug 11 '14 at 10:59
  • If you are new to Wordpress, I recommend you to go with WP_Query. query_posts requires a lot of attention as well as the per_get_posts hook. What isn't working with this query? – Tomás Cot Aug 11 '14 at 11:40
  • This query returns all the bananas, not only the ones that have a size larger then (or equal to) $_GET['size']. Which I find strange. – user57948 Aug 11 '14 at 14:48
  • can you do a var_dump of var_dump($_GET['size']) just in case, and of this: $my_query->request. Then add it to the question – Tomás Cot Aug 11 '14 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy