Home Forums General Issues Another Issue with Filtering by Multiple Keys


Another Issue with Filtering by Multiple Keys

  • So this has been driving me up a wall. I have custom fields for $city and $state. City is a text field and State is a Select field.

    I am trying to filter posts that match a city AND state. I have been able to filter all posts by $city to get all posts that are tagged Springfield, for example, which gives me any results that are tagged with Springfield in any city. But what I need to do is filter posts that match say Springfield AND Illinois to not show results from say, Springfield, Idaho.

    I’ve read and tested a lot of things and nothing seemed to let me filter by both of these keys. Including switch State from Select to Text and hardcoding some of these values in.

    Here is my current code that works to filter but only by city:

    $cityarray = array('Springfield, Illinois','Springfield, Idaho');
    foreach ($cityarray as $current_cityarray) {
        $exploded_city = explode(", ",$current_cityarray);
        $current_city = $exploded_city[0];
        $current_state = $exploded_city[1];
        $pages = get_pages(array(
      'meta_key' => 'city',
      'meta_value' => $current_city,
    foreach($pages as $page){
        echo "<h2>".$current_city.", ".$current_state."</h2><div>";
        $id = $page->ID;
    echo get_the_title($id).'</div>';      

    I’ve tried to put it into a meta_query as follows but it shows no results.

    foreach ($cityarray as $current_cityarray) {
        $exploded_city = explode(", ",$current_cityarray);
        $current_city = $exploded_city[0];
        $current_state = $exploded_city[1];
        $pages = get_pages(array(
        'meta_query' => array(
                'relation' => 'AND',
                'key'     => 'city',
                'value'   => $current_city,
                'key'     => 'state',
                'value'   => $current_state,

    What am I doing wrong? I’ve scoured for answers but nothing I’ve tried had results. I have to be missing something. Is this where I have to bring in a JOIN? Is it something to do with serialization of Select fields? I don’t know!!

  • I should clarify only one city, state combination is being passed to it at a time. The way I wrote in the cityarray implies I do want to see both Illinois and Idaho. My bad.

  • You said that the state field is a select field? What are the values of the select field? Are they text values or are they some type of relationship with something else?

  • The state select field is simply a list of all US states, i.e.


    I may have made this more complicated than it needs to be including the array portion of my other function. Let’s make this simpler.

    I’m passing a $city and $state variable to this code.

    I need to loop through and display all pages that have ACF values equal to these $city AND $state variables. Right now I am able to match ONLY the city or ONLY the state, but not both.

  • You could ignore the array portion of my original code and replace

        $current_city = $exploded_city[0];
        $current_state = $exploded_city[1];


        $current_city = 'Springfield';
        $current_state = 'Missouri';
  • The fields in question are more important than the values. It looks like your using the right value. if the state select is a single value and the city is a text field and the values you’re using match the values in the DB then I don’t see anything in the meta_query you posted.

    I am not too sure about using ‘get_pages()’ though, are you getting “Pages” the default post type when using this function is “page”. If not then you need to specify the post type.

  • Right, based on everything I’ve seen online, my code should work (that’s what you’re saying correct?). I’ve even tried switch the state from a select to a text field to see if it matches that way to no avail.

    I’m actually searching for pages not posts. These pages have been assigned a template I named ‘Client Pages’. Each page is a different company (using the same template), which asks them for their city (which is entered by the user) and their state (which is selected from a drop-down) in the wp dashboard. I don’t ever specify that it has to match ‘Client Pages’ in my code because I am happy to search through all pages, not posts, for this information since there are very few pages other than these company pages.

  • That’s what I’m saying, it should be working unless there is something about the fields other than what you’ve posted.

    These fields are top level fields? Not sub fields in a group field?

  • Yup Company Field Group > City is a required Text field and State is a required Select field. Can’t even hardcode the values to match both. Again, I can match anything from State or anything from City but not both. It’s been driving me crazy.

  • I have some other loops and functions going on but the fact I can match either one means my loops should be right 😐

  • I think I’ve found a possible issue. get_pages() does not support “meta_query” from the information that I’m finding.

    You need to switch to get_posts() or WP_Query().

  • Wow! That was it. All I had to do was change get_pages to get_posts and add

    'post_type' => 'page',

    Thank you so much!!

  • 🙂 Seems so. Sorry it took so long to get there. I don’t use either get_posts() or get_pages() and instead I always use WP_Query() directly.

Viewing 13 posts - 1 through 13 (of 13 total)

The topic ‘Another Issue with Filtering by Multiple Keys’ is closed to new replies.