0

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.

  • Note that queries that say != or NOT are super expensive, and that post meta wasn't designed for these kinds of queries. If it was then categories and tags would be stored in post meta. Use a taxonomy instead if you need to find things – Tom J Nowell Nov 4 at 17:28
  • Also, can you provide some context for this? Why do they have to be distinct? By trying to make things as generic as possible and hide what you're doing you've made your Q much more difficult to answer. There may be an alternative API that gives you what you want that's also easier and faster, but we would never know due to the lack of context :( – Tom J Nowell Nov 4 at 17:29
  • Thanks for your comment. Nothing special just want to show unique posts in order to remove duplicates. – stefanosn Nov 4 at 17:32
0

You can simply limit the query to return only one result. To achieve this, use the field posts_per_page in your query arguments. Check the documentation for more info on this.

$args = [
    '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',
    'posts_per_page' => 1,
];

$wp_query = new WP_Query($args);

Sidenote: To make your code more readable, try to stick to one thing. Using short array syntax ([ ]) somewhere? -> Use it everywhere and avoid array(). Using 'key' => 'value'? Don't use 'key'=>'value' somewhere else.

  • Thanks for your response. But i need to populate data in pages usings pagination and each page has to include 16 posts so this is not an option. Any other way using wp_query? – stefanosn Nov 4 at 17:16
  • @stefanosn Ah ok, sorry I misunderstood. I think in this case it is best to write your own query. Can you edit your question to show your current full code, including pagination? – kero Nov 4 at 18:21
  • After many hours found it. I needed to use the filter function for groupby. function query_group_by_filter($groupby){ global $wpdb; return ' mt1.meta_value '; } add_filter('posts_groupby', 'query_group_by_filter'); Now i have another issue. I have to find a way to get the TOP 1 most recent post by post_date for each groupby of the meta_value. Any thoughts how to achieve TOP 1 in using wp_query? – stefanosn Nov 5 at 1:57

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.