WP_query minimum post results - php

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'
);

Related

Foreach loop inside WP filtering system

I am trying to create custom filters for my users. Here is what i am trying to do which doesn't work:
$args = array(
'post_type' => 'books',
'meta_key' => 'product_title',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'posts_per_page' => -1,
'relation' => 'OR',
'options' => array()
);
foreach ($myurlfilter as $multifilter) {
$args['options'][] = array (
'key' => 'product_title',
'value' => "$multifilter",
'compare' => 'LIKE',
);
}
I don't get any php error and i don't know what i am doing wrong. I did made my code according to this answer: Foreach loop inside array and if you check the last comment of the author at below answer it say "code does not work".
From the output i see, i believe the options from foreach code are not "connecting" properly with code above it.
If that helps what i am trying to achieve with foreach is something like this, which works perfect:
$args = array(
'post_type' => 'books',
'meta_key' => 'product_title',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'product_title',
'value' => 'Leonardo',
'compare' => 'LIKE',
),
array(
'key' => 'product_title',
'value' => 'Einstein',
'compare' => 'LIKE',
)
)
);
Try building your meta query with the starting "or"
$myMeta = array( 'relation' => 'OR' );
foreach ($myurlfilter as $multifilter) {
$myMeta[] = array (
'key' => 'product_title',
'value' => $multifilter,
'compare' => 'LIKE',
);
}
$args['meta_query'][] = $myMeta;
I also belive you need to ditch "options" and add to "meta_query"
$args = array(
'post_type' => 'books',
'meta_key' => 'product_title',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => array() // change this, remove the relation
);

Visual composer custom query - excluding meta_key

I have a custom query that I would like some help converting to visual composer's custom query. Basically, I would like to exclude all posts from displaying in the post grid that have the meta_key: _is_featured_posts and its value as yes.
// WP_Query arguments
$args = array(
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'nopaging' => false,
'posts_per_page' => '12',
'order' => 'DESC',
'orderby' => 'date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
'compare' => 'NOT EXISTS',
),
),
);
// The Query
$query = new WP_Query( $args );
Any help would be appreciated.
Thanks
There is an alternative solution, it is not recommended but as NOT EXISTS is not working so you can use the following code. I also check the solution given here, but it's not working either.
//to hold the post id which has _is_ns_featured_post => 'yes'
$exclude_id = array();
$args_exclude = array(
'post_type' => array('post'),
'post_status' => array('publish'),
'posts_per_page' => '-1',
'meta_query' => array(
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
),
),
);
$exclude_posts = new WP_Query($args_exclude);
if (!empty($exclude_posts->posts))
{
foreach ($exclude_posts->posts as $post)
{
$exclude_id[] = $post->ID;
}
}
$args = array(
'post_type' => array('post'),
'post_status' => array('publish'),
'nopaging' => false,
'posts_per_page' => '12',
'order' => 'DESC',
'orderby' => 'date',
'post__not_in' => $exclude_id //exclude post_id which has _is_ns_featured_post => 'yes'
);
// The Query
$query = new WP_Query($args);
foreach ($query->posts as $post)
{
print_r($post);
}
Hope this helps!
See: visual composer wordpress query for post grid
Try this:
$args = array(
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'nopaging' => false,
'posts_per_page' => '12',
'order' => 'DESC',
'orderby' => 'date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
'compare' => 'NOT EXISTS',
),
),
);
echo http_build_query($args);
// Result:
post_type%5B0%5D=post&post_status%5B0%5D=publish&nopaging=0&posts_per_page=12&order=DESC&orderby=date&meta_query%5Brelation%5D=AND&meta_query%5B0%5D%5Bkey%5D=_is_ns_featured_post&meta_query%5B0%5D%5Bvalue%5D=yes&meta_query%5B0%5D%5Bcompare%5D=NOT+EXISTS
http://sandbox.onlinephpfunctions.com/code/5c2bc6ddd37a02fc8facf4f227176e262854b92e
I would recommend to avoid use array('post') in case if only one post type, so just use post_type=post&post_status=publish&nopaging=0&posts_per_page=12&order=DESC&orderby=date&meta_query[relation]=and&meta_query[0][key]=_is_ns_featured_post&meta_query[0][value]=yes&meta_query[0][compare]=NOT EXISTS
P.S.
possibly %5B and %5D you will need to convert back to [ and ] via echo urldecode(http_build_query($args));

Wordpress WP_Query with multiple meta_values for a meta_key

I'm trying to run a WP_Query in order to search for all the products in my database with multiple meta values.
e.g
Product 1 -> meta_key['key1'] ->meta_value['value1']
Product 2 -> meta_key['key1'] ->meta_value['value2']
Product 3 -> meta_key['key1'] ->meta_value['value3']
So i want to get all three products.My arguments are
$args = array(
'post_type' => 'product',
'posts_per_page' => 2,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'key1',
'value' => 'val1',
'compare' => '='
),
array(
'key' => 'key1',
'value' => 'val2',
'compare' => '='
),
array(
'key' => 'key1',
'value' => 'val3',
'compare' => '='
),
),
'paged' => $paged
);
The problem is that no products are returned. Instead , if a give only one meta_key => meta_value pair it works fine
$args = array(
'post_type' => 'product',
'posts_per_page' => 2,
'orderby' => 'title',
'order' => 'ASC',
'meta_key' => 'key1',
'meta_value' =>'val1',
'paged' => $paged
);
You should use IN as compare value, like this:
$args = array(
'post_type' => 'product',
'posts_per_page' => 2,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'key1',
'value' => array('val1','val2','val3'),
'compare' => 'IN',
),
),
'paged' => $paged
);
Hope it helps!

Order wp_query by numeric custom field

I'm trying to allow users to sort a list of results from a wp_query by a numeric custom field value. Users can click a button to change the sort by values, but when Price is selected, this is what the wp_query looks like:
'meta_key' => 'adult',
'orderby' => 'meta_value_num',
'order' => 'ASC',
The field is the numeric field from ACF (ACF 5 beta is being used) with a step size of 0.01 to allow for prices like 9.99.
However, if I run this query it returns no results every time, even when there are definitely results that should be matching.
Any ideas where I'm going wrong?
EDIT - full query
$keywordString = $_SESSION['search']['keyword'];
$keywords = explode(', ', $keywordString);
$taxQuery = array(
'relation' => 'AND',
array (
'taxonomy' => 'main-cat',
'field' => 'slug',
'terms' => $_SESSION['search']['cat']
)
);
if( $_SESSION['search']['keyword'] != '' ) {
$taxQuery[] = array(
'taxonomy' => 'sub-cat',
'field' => 'name',
'terms' => $keywords
);
}
$args = array(
// general
'post__in' => $postIDs,
'post_type' => 'event',
'posts_per_page' => 10,
'paged' => $paged,
'cache_results' => false,
'meta_key' => $_SESSION['search']['sort-key'], // becomes 'adult'
'orderby' => $_SESSION['search']['sort-by'], // becomes 'meta_value_num'
'order' => 'ASC',
// category filter
'tax_query' => $taxQuery,
// date filter
meta_query' => array(
'relation' => 'AND',
array(
'key' => 'date_%_start-date',
'value' => $when,
'compare' => '>=',
'type' => 'NUMERIC'
),
array (
'key' => 'date_%_end-date',
'value' => $when2,
'compare' => '<=',
'type' => 'NUMERIC'
)
)
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );

Order by One Custom Field Value and Select Posts From Another With Wordpress

I'm trying to only show posts that are marked as in-stock and order them by their inventory_number (which is a number value so I'm using meta_value_num). The code below is selecting in-stock items, but it isn't ordering the posts by inventory_number. What am I doing wrong?
$args = array(
'numberposts' => -1,
'post_status'=>"publish",
'post_type'=>"post",
'category_name'=>"tape",
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'inventory_number',
'orderby' => 'meta_value_num',
'order' => 'asc'
),
array(
'key' => 'status',
'value' => 'in-stock',
'compare' => 'LIKE'
)
)
);
For order by on a custom field, meta_key=keyname must be present in the query. Plus I don't think you want the order by in the AND clause. So try this...
$args = array(
'numberposts' => -1,
'post_status' => 'publish',
'post_type' => 'post',
'category_name' => 'tape',
'meta_query' => array(
array(
'key' => 'status',
'value' => 'in-stock',
'compare' => 'LIKE'
)
),
'meta_key' => 'inventory_number',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);

Categories