I’m currently looking into a solution to make a search query based on a minimum number of results, also when not all search options match.
Ideal situation;
I have made a custom post type and added a couple of shoes with different colors and sizes. And I am querying with WP_Query() for ‘blue shoes’ and ‘size 8’. When I do this query, I only get 3 blue-colored shoes in size 8. Now I want to add 7 more equal shoes in size 8 to get a total of 10 shoes.
How would I do this within Wordpress in combination with Advanced Custom Fields?
I have made a custom WP_Query based on the WP_query() method. And made an array to store all post-results based on the first query and if the length of that array is not equal to 10 it will do an additional query.
So far so good, but then I get duplicate results:
=> I get the 3 already found blue-colored shoes in size 8, and again in the second query all the size 8 shoes including the already found shoes (from the first query).
See my code below;
$shoe_array = array();
$args = array(
'numberposts' => -1,
'post_type' => 'my_shoes',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'size',
'value' => 8,
'compare' => 'LIKE'
),
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'LIKE'
),
),
'orderby' => 'date',
'order' => 'DESC'
);
$shoes = new WP_Query( $args );
$posts = $shoes->posts;
foreach($posts as $post) {
array_push($shoe_array, $post->post_name);
}
if (count($shoe_array) > 9) {
echo 'Result of found shoes: ';
print_r($shoe_array);
} else {
$args = array(
'numberposts' => -1,
'post_type' => 'my_shoes',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'size',
'value' => 8,
'compare' => 'LIKE'
)
),
'orderby' => 'date',
'order' => 'DESC'
);
$extra_shoes = new WP_Query( $args );
$posts = $extra_shoes->posts;
foreach($posts as $post) {
array_push($shoe_array, $post->post_name);
}
echo 'Result of found shoes: ';
print_r($shoe_array);
}
Anyone know how to make the query 'smarter'?
Please feel free to ask me additional questions if needed. Thanks! :-)
I hope that code can help you :
<?php
$shoe_array = array();
$args = array(
'numberposts' => -1,
'post_type' => 'my_shoes',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'size',
'value' => 8,
'compare' => 'LIKE'
),
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'LIKE'
),
),
'orderby' => 'date',
'order' => 'DESC',
'fields' => 'ids'
);
$shoes = get_posts( $args ) ;
$count10 = 10 - count($shoes) ;
$args2 = array(
'numberposts' => -1,
'post_type' => 'my_shoes',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'size',
'value' => 8,
'compare' => 'LIKE'
)
),
'orderby' => 'date',
'order' => 'DESC',
'post__not_in' => $shoes,
'posts_per_page' => $count10 ,
'fields' => 'ids'
);
$shoes8 = get_posts( $args2 ) ;
$post_ids = array_merge( $shoes, $shoes8);
print_r($post_ids);
?>
So just add a does not equal condition to color is blue
$args = array(
'numberposts' => -1,
'post_type' => 'my_shoes',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'size',
'value' => 8,
'compare' => 'LIKE'
)
array(
'key' => 'color',
'value' => 'blue',
'compare' => '!='
),
),
'orderby' => 'date',
'order' => 'DESC'
);