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 );
Related
My issue is as follows;
I have fields with no value showing on my page - I want them to be hidden, until they have a value. In my code I have a meta_value_num as a sorting option per today. But it still shows values that are 0(zero) and blanks.
Please tell me how I can filter these so that the empty ones are not shown. E.g. if I can sort everything with a value = 100 and above, that would be good enough.
This is how the code looks per today:
$tax = $wp_query->get_queried_object();
$new_query = new WP_Query( array(
'post_type' => 'clinic',
'meta_key' => $treatment,
'orderby' => 'meta_value_num',
'order' => 'DSC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'location',
'field' => 'ID',
'terms' => $tax->term_id
)
),
));
I managed to fix the filter issue.
By adding this code it now filters out everything below 20 and shows all above. Big shout out to #Cbroe for pointing me in the right direction!
'meta_value' => '20',
'meta_compare' => '>=',
Code now looks like this;
$tax = $wp_query->get_queried_object();
$new_query = new WP_Query( array(
'post_type' => 'clinic',
'meta_key' => $treatment,
'meta_value' => '20',
'meta_compare' => '>=',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'location',
'field' => 'ID',
'terms' => $tax->term_id
)
),
));
How can I show user and order them by 2 meta fields? For example I have user query
$args = array(
'role' => 'specialist',
'meta_key' => 'proffession',
'meta_value' => $term->ID,
'meta_compare' => '=',
'number' => 20,
'paged' => $paged,
);
$user_query = new WP_User_Query($args);
I want to sort this query by two meta_values, for example in PHP:
if(get_usermeta( $user_id, $meta_key = 'be_first' )>time() AND get_usermeta( $user_id, $meta_key = 'pro_date' )>time())
// show this users first ordered by be_first DESC, ordered by pro_date
if($pro_date>time()){
//show this users second ordered by pro_date desc
}
//show all other users
You can try this... I don't have your fields, so this is not tested. You can create a meta_query where your clauses are set to compare exists then use those to orderby.
$args = array(
'role' => 'specialist',
'number' => 20,
'paged' => $paged,
'meta_query' => array (
'relation' => 'AND',
array(
'key' => 'profession',
'value' => '$term->ID',
'compare' => '=',
),
'be_first_clause' => array(
'key' => 'be_first',
'compare' => 'EXISTS',
),
'pro_date_clause' => array(
'key' => 'pro_date',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'be_first_clause' => 'ASC',
'pro_date_clause' => 'ASC'
)
);
$user_query = new WP_User_Query($args);
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'
);
I'm having a bit of trouble returning posts with multiple taxonomies and terms. Hoping someone with far more knowledge than myself can help me understand.
I'm using a select menu to populate the select options with taxonomy information of a page (in this case, the Products page). All is good with a single taxonomy and term in the tax_query but as soon as I try to use an array to pass multiples, i'm no longer returning anything. This seems simple enough but I'm missing something. Any ideas?
Here is what I'm working with:
$producttype = $_GET['ProductType'];
$businessunit = $_GET['BusinessUnit'];
$products = new WP_Query( array(
'post_type' => 'products',
'posts_per_page' => 15,
'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'producttype',
'field' => 'name',
'term' => $producttype
),
array(
'taxonomy' => 'businessunit',
'field' => 'name',
'term' => $businessunit
)
)
)
You error is a key array tax_query => term should be terms
$producttype = $_GET['ProductType'];
$businessunit = $_GET['BusinessUnit'];
$products = new WP_Query( array(
'post_type' => 'products',
'posts_per_page' => 15,
'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'producttype',
'field' => 'name',
'terms' => $producttype
),
array(
'taxonomy' => 'businessunit',
'field' => 'name',
'terms' => $businessunit
)
)
Reference wordpress
I have a wordpress site using woocommerce.
I am trying to use filter the products using WP_query to show just 4 products which are on sale, and which are also only in a certain category.
I am using meta_query to only filter in the sale products, and tax_query to filter the category.
I am struggling to get the loop to work using both tax_query and meta_query, although either of those without the other are working fine.
(I have also tried using 'product_cateogory' => 'skateboard-footwear' in the arguments, but the result is the same).
Here's the code:
$args = array(
'post_type' => 'product',
'orderby' => 'date',
'order' => 'desc',
'posts_per_page' => '4',
'meta_query' => array(
array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'skateboard-footwear',
'operator' => 'IN'
)
)
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
$args = array(
'post_type' => 'product',
'orderby' => 'date',
'order' => 'desc',
'posts_per_page' => '4',
'meta_query' => array(
array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => '27',
'operator' => 'IN'
)
)
);
Need to add one more array thats specified min variation sale price and then after use tax_query. And also use product category by its id.