Support

Account

Home Forums Front-end Issues WP_Query search results with 's' and 'meta_query' at the same time.

Solved

WP_Query search results with 's' and 'meta_query' at the same time.

  • I’m looking to use WP_Query with a custom post type to create a search results page that looks for the search query within either the post title OR an ACF text field.

    I’ve been able to get both methods working independently before on previous projects, so I know how to search ACF values (helpful link: http://www.advancedcustomfields.com/resources/how-to-query-posts-filtered-by-custom-field-values/) but I can’t seem to find anything with Google about searching either the post title OR the ACF field at the same time.

    I can get it working okay with just the post title…

       $args = array(
            's' => $s,
            'numberposts' => -1,
            'post_type' => 'unq_artist_gallery'
        );

    I can get it working okay with just an ACF value…

       $args = array(
            'numberposts' => -1,
            'post_type' => 'unq_artist_gallery',
            'meta_query' => array(
                array(
                    'key' => 'county',
                    'value' => $s,
                    'compare' => 'LIKE'
                )
            )
        );

    What I can’t do is find a way to combine the two.

    I’ve tried a couple of methods…

       $args = array(
            's' => $s,
            'numberposts' => -1,
            'post_type' => 'unq_artist_gallery',
            'meta_key' => 'county',
            'meta_value' => $s
        );
       $args = array(
            's' => $s,
            'numberposts' => -1,
            'post_type' => 'unq_artist_gallery',
            'meta_query' => array(
                array(
                    'key' => 'county',
                    'value' => $s,
                    'compare' => 'LIKE'
                )
            )
        );
       $args = array(
            's' => $s,
            'numberposts' => -1,
            'post_type' => 'unq_artist_gallery',
            'meta_query' => array(
                'relation' => 'OR',
                array(
                    'key' => 'county',
                    'value' => $s,
                    'compare' => 'LIKE'
                )
            )
        );

    None of these work, I thought the final one would but the ‘OR’ operator seems to only compare between multiple ACF values rather than anything in the overall query. (Which kinda makes sense actually.)

    Does anyone know of a way to combine ‘s’ with a ‘meta_query’ for the same query?

    Cheers.

  • Hi,

    I was wondering the same thing and found the answer here: http://wordpress.stackexchange.com/questions/78649/using-meta-query-meta-query-with-a-search-query-s

    It has a few extra steps but gets the job done.

    Also, I noticed a little hiccup in my case. When the search was supposed to return nothing, it returned all posts. I solved the problem by wrapping the loop between an IF statement, like:

    if(!empty($unique)) {
    //wp_query here
    }

    For some reason, wp_query was returning all posts when post__in was an empty array.

    Hope that helps!

  • Ohhh array_merge() and array_unique(), of course. Here I am looking for WordPress / ACF functions to do the job when vanilla php functions were the answer.

    Thank you so much, that solved the problem perfectly.

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

The topic ‘WP_Query search results with 's' and 'meta_query' at the same time.’ is closed to new replies.