wp query - Use WP_query to match post types based on custom field values

admin2025-01-07  6

i'm trying to query posts from the post type Product that match with the current post in the loop from the post type Item, based on the custom fields product-ean and item-ean.

For example: i have a Item post with item-ean 123 and 2 Product posts with product-ean 123. Now i would like to show both Product posts on the Item post.

Help is greatly appreciated, thank you.

Menno

i'm trying to query posts from the post type Product that match with the current post in the loop from the post type Item, based on the custom fields product-ean and item-ean.

For example: i have a Item post with item-ean 123 and 2 Product posts with product-ean 123. Now i would like to show both Product posts on the Item post.

Help is greatly appreciated, thank you.

Menno

Share Improve this question asked Jan 11, 2020 at 9:58 MennoBMennoB 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

In the WP Docs you can find the answer.

I have not tested this snippet, but it should become something like this:

$args = array(
    'post_type'  => 'product',
    'meta_query' => array(
        'relation' => 'AND',
        array(
                'key' => 'product-ean',
                'value' => 123,
                'compare' => '=',
        ),
        array(
                'key' => 'item-ean',
                'value' => 123,
                'compare' => '=',
        ),
    ),
);
$query = new WP_Query( $args );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265487a1049.html

最新回复(0)