WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom post type with custom fields, the one in particular is "zip". I also have an array that stores like 20 different zip codes. I want to check to see if any of those custom post types have one of those zip codes. If they do, then I want them to appear in the search results. If they don't, I don't want them to appear. How would I go about doing this..? It's driving me insane. Thanks in advance.

share|improve this question
up vote 1 down vote accepted

You will need to create two queries. The first will be one that pulls all of the custom post types in the search query.

    $args = array(
        'post_type' => YOUR-POST-TYPE,
        'posts_per_page' => '-1',
        's' => get_search_query(),
    $sort_query = new WP_Query($args);
    $included_results = array();

You will then have to probably print out your results so you can find where the zip code is in the array.

Run the loop, checking if the zip is in your zip code array (in_array()) and add the id to the included results array (ex: $included_results[] = $post->ID)

Next, you will want to end and reset the query, and create a new query using the same args as before, but adding 'post__in' => $included_results

share|improve this answer
I've done something similar to this, but credit has to be given to you for getting me going in the right direction. I edited the search.php template file, and did some work on the pre_get_posts filter. Overall, I was able to achieve what I was looking for. Thank you! – willbeeler Aug 21 '13 at 9:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.