0
votes

I have a listing of custom post types and I'm trying to implement a search form over it. The search form will simply push url queries that will be caught by the same page and depending on these queries (such as name="..", order="..", etc.), the list of custom post types will represent these characteristics.

I'm especially stuck with implementing the arguments for WP_Query that will allow me to search through the meta data for each custom post type. Here is the structure of my $args string (as called by print_r - this will not display the exact syntax i've used, as it's mixed with quite abit of logic, but this gives a good idea of how my arguments are interpreted):

[post_type] => obituary
[post_per_page] => 25
[meta_query] => Array
    (
        [relation] => OR
        [0] => Array
            (
                [meta_key] => last_name
                [meta_value] => Green
            )

        [1] => Array
            (
                [meta_key] => first_name
                [meta_value] => Green
            )

    )

If i remove key array[meta_query][0] (so the array defining the value to look as last_name (hence only looking under one meta data key), then the query will work OK. I thought of adding the relation key and value OR to be able to find the meta_value is either last_name OR first_name meta keys. However, this doesn't seem to work, the query still returns the full list of custom post types.

As I am new to this, is there anything flagrant I'm missing? I've looked around through the docs I could find and this, imo, should work.

What are your thoughts?

Edit: A correction to the above: search under only one meta_key will only work if this is defined outside the meta_query.

1

1 Answers

0
votes

Figured it out. The right structure is as follows:

(
    [post_type] => obituary
    [post_per_page] => 25
    [meta_query] => Array
        (
            [relation] => OR
            [0] => Array
                (
                    [key] => last_name
                    [value] => Green
                    [compare] => LIKE
                )

            [1] => Array
                (
                    [mkey] => first_name
                    [value] => Green
                    [compare] => LIKE
                )

        )

)

The main difference is that the meta_key and meta_value are called key and value when within the meta_query array.