0

Below code return the same results. Is it any difference between them and if it is then which one is better?

code snippet 1:

$args = array(
    'post_type' => 'product',
    'meta_key' => 'location',
    'meta_value' => 'Melbourne'
);

$query = new WP_Query( $args );

code snippet 2:

$args = array(
    'post_type'  => 'product',
    'meta_query' => array(
        array(
            'key'     => 'location',
            'value'   => 'Melbourne'
        ),
    ),
);
$query = new WP_Query( $args );

1 Answer 1

0

There is no difference in terms of the "final" query. The former was the "old" way of querying post meta, before WP_Meta_Query was introduced with support for more complex queries.

WP_Query::get_posts() now simply translates meta_key/value arguments into the new format with WP_Meta_Query::parse_query_vars().

Use whichever format you feel suits best for the task.

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.