0

I am having issues with a meta query on a custom post type.

The following arguments are set:

$args = array(
        'post_type'     => 'wp_comp_entries',
        'posts_per_page' => -1,
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key'     => 'wp_comp_entry_competition-id',
                'value'   => $_POST['competition-id'],
                'compare' => '=',
            ),
            array(
                'key'     => 'wp_comp_entry_email',
                'value'   => $_POST['email'],
                'compare' => '=',
            ),
        ),
    );

A print_r of this looks like:

Array
(
    [post_type] => wp_comp_entries
    [posts_per_page] => -1
    [meta_query] => Array
        (
            [relation] => AND
            [0] => Array
                (
                    [key] => wp_comp_entry_competition-id
                    [value] => 972
                    [compare] => =
                )

            [1] => Array
                (
                    [key] => wp_comp_entry_email
                    [value] => [email protected]
                    [compare] => =
                )

        )

)

Which is great. I then run

$lookup = new WP_Query( $args );

The start of a print_r of $lookup looks like:

WP_Query Object
(
    [query_vars] => Array
        (
            [post_type] => wp_comp_entries
            [posts_per_page] => -1
            [meta_query] => Array
                (
                )

As you can see the meta_query is empty. I have no idea what is causing this as at the bottom of that dump there is this:

[query] => Array
(
    [post_type] => wp_comp_entries
    [posts_per_page] => -1
    [meta_query] => Array
        (
            [relation] => AND
            [0] => Array
                (
                    [key] => wp_comp_entry_competition-id
                    [value] => 972
                    [compare] => =
                )

            [1] => Array
                (
                    [key] => wp_comp_entry_email
                    [value] => [email protected]
                    [compare] => =
                )

        )

)

Which shows that my code is recognised, WP just isn't feeding it into it's query. I am lost and looking for any direction on why this could not be working?

-- EDIT --

I must add I have tried the standard query from the Codex with no joy either.

7
  • I don't see anythin wrong. Can you check that the entry with that meta keys and meta values exists?
    – cybmeta
    Commented Mar 26, 2015 at 11:50
  • 1
    In the print_r output of $lookup the WP_Query object, look for the request property and post it here. That's the MySQL statement itself, it should tell us more. Commented Mar 26, 2015 at 12:23
  • @SeventhSteel [request] => SELECT sosen_posts.* FROM sosen_posts WHERE 1=1 AND sosen_posts.post_type = 'wp_comp_entries' AND (sosen_posts.post_status = 'publish' OR sosen_posts.post_status = 'future' OR sosen_posts.post_status = 'draft' OR sosen_posts.post_status = 'pending' OR sosen_posts.post_status = 'private') ORDER BY sosen_posts.post_date DESC
    – Dan Taylor
    Commented Mar 26, 2015 at 15:15
  • @cybmeta yes there are entries with the meta details. The query returns all posts as there is no other filter. If I use the single filter i.e. use meta_key = etc then it works but I need to filter on two meta keys.
    – Dan Taylor
    Commented Mar 26, 2015 at 15:17
  • Looks like it's stripping out the entire meta query for some reason. It might be choking on the hyphen in wp_comp_entry_competition-id? Try it without that part. Commented Mar 26, 2015 at 16:24

1 Answer 1

0

OK, I have made a basic error here.

I have hooked into parse_query and made the array blank if certain paramenters are met, which they where here.

Thanks for your help guys, you led me to where to look.

1
  • So, your problem was that you was stripping the meta_query in pare_query hook? If you need help making the hook working for the main query but not for secondary queries, let us know.
    – cybmeta
    Commented Mar 27, 2015 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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