0

I want to get all posts having the same meta values.

My Post A has 2 meta values: ID 182 and ID 183

I want to have all post having meta 182 or 183, excepting the current one of course.

I have tried:

$metaSearch = [
    'relation' => 'OR'
];

// tags are the tags (meta tag_initiative) of current post
if ($tags) {
    foreach ($tags as $tag) {
        $metaSearch [] = [
            'key' => 'tag_iniative',
            'value' => $tag->term_id,
            'compare' => '=',
        ];
    }
}
// array(2) { ["relation"]=> string(2) "OR" [0]=> array(3) { ["key"]=> string(12) "tag_iniative" ["value"]=> int(182) ["compare"]=> string(1) "=" } }
var_dump($metaSearch);

$initiativesOther = get_posts([
    'orderby' => 'publish_date',
    'order' => 'DESC',
    'post_type' => 'initiative',
    'posts_per_page' => 4,
    'exclude' => [get_the_ID()],
    'meta_query' => $metaSearch,
]);

This won't bring me any post, but:

$initiativesOther = get_posts([
    'orderby' => 'publish_date',
    'order' => 'DESC',
    'post_type' => 'initiative',
    'posts_per_page' => 4,
    'exclude' => [get_the_ID()],
    'meta_query' => [
        'key' => 'tag_iniative',
        'value' => 182,
        'compare' => '=',
    ],
]);

Will work and shows me an other post. How can I do that ?

1 Answer 1

0

you can set the meta like this

'meta_query' => [
    'key' => 'tag_iniative',
    'value' => [182,183],
    'compare' => 'IN',
]

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.