0

I need to pass dynamic value in to the value key in the meta_query array in the wp_query. Is it possible to pass the dynamic value in the query. My Code is

$prodPost = new WP_Query( array(
    'post_type'         => 'product',
    'posts_per_page'    => 6,
    'paged'             => 1,
    'meta_query'        => array(
        array(
            'relation'  => 'AND'
        ),
        array(
            'key'       => 'product_last_date',
            'value'     => date('d-m-Y'),
            'compare'   => '>',
        ),
        array(
            'key'       => 'product_limit',
            'value'     => $limit,
            'compare'   => '>',
        ),
    ),
) );

As you see in my query i need to query the product which satisfies both condition, but the problem with second condition is that I need to pass the $limit value dynamically for the corresponding product post. Is there any to accomplish this query. Thanks in advance

1

You should be able to do what you're trying to do. I think the issue may be that relation should not be contained in it's own array. It should look like this:

$prodPost = new WP_Query( array(
    'post_type'         => 'product',
    'posts_per_page'    => 6,
    'paged'             => 1,
    'meta_query'        => array( array(
        'relation'  => 'AND',
        array(
            'key'       => 'product_last_date',
            'value'     => date('d-m-Y'),
            'compare'   => '>',
        ),
        array(
            'key'       => 'product_limit',
            'value'     => $limit,
            'compare'   => '>',
        ),
    ),
) );

If it's still not working at that point I would verify your meta keys are the correct keys and $limit holds the expected value.


See Initializing Meta Query for more examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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