Site icon Hip-Hop Website Design and Development

Get unique post by meta value using wp_query

I use wp_query to retrieve my posts by using a meta value. One of the meta values have the same text stored in the database. Let me give an example.

Post title  Meta value
hello1         text1
hello2         text2 
hello3         text2

So in the database exist three posts. The first one has ‘text1’ in the meta value. The other two have ‘text2’ stored in the meta value.

The query is working but i need to return distinct posts based on the meta value.
So the result should be

hello1        text1
hello2        text2

the third post i dont want to show up because it has the same meta value as the post with the title hello2. So only unique meta value posts i would like to show up as you understand.

How is this possible using wp_query?

This is my code so far which returns all non empty posts based on meta_value text.

$args = array(

            'cat'=>'44',
        'posts_per_page'=>'16',
        'post_type' => 'post',
        'post_status' => 'publish',

        'meta_query' => [
            'metavalue1' => [ 'key' => 'Meta_value', 'value' => '', 'compare' => '!=' ],
            'price1'=> [ 'key' => 'price_euro', 'value' => '30', 'compare' => '>','type'=>'NUMERIC' ],
],
    'orderby' => 'price1',
    'order' => 'ASC'
    );

    $wp_query = new WP_Query($args);

Any help appreciated. I use this code in my Search.php file responsible for searches.