wp query - Get unique post by meta value using wp_query

admin2025-01-07  4

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.

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.

Share Improve this question asked Nov 4, 2019 at 17:05 stefanosnstefanosn 1339 bronze badges 3
  • 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 Commented Nov 4, 2019 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 Commented Nov 4, 2019 at 17:29
  • Thanks for your comment. Nothing special just want to show unique posts in order to remove duplicates. – stefanosn Commented Nov 4, 2019 at 17:32
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265289a1034.html

最新回复(0)