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.
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.
!=
orNOT
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