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
)
),
));
Related
I've got a little bit of code that returns some info from a CPT (pre_jobs) when a certain taxonomy is selected (repeat) and sorts it by an ACF date field (pre_job_due_date)
What I'd like to do is filter these results so that only the ones show that are in the near future - the idea is that we can contact the client to book the work in, say within 6 weeks of today's date, then change to a different taxonomy when we're awaiting a reply or the job gets booked. Basically a custom to-do list. How would I add a filter that shows these? Some jobs will repeat in 5 years time so I don't want a massive list. So far I've got:
$posts = get_posts(array(
'post_type' => 'pre_jobs',
'posts_per_page' => -1,
'meta_key' => 'pre_job_due_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'pre_job_status',
'field' => 'slug',
'terms' => array( 'repeat' )
),
),
));
Thanks in advance
After calculating the date in 6 weeks with Php, you can use the wp meta_query argument in your query
$before_date = date("Ymd", strtotime("+6 weeks"));
$posts = get_posts(array(
'post_type' => 'pre_jobs',
'posts_per_page' => -1,
'meta_key' => 'pre_job_due_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'pre_job_due_date',
'value' => $before_date,
'compare' => '<',
),
),
'tax_query' => array(
array(
'taxonomy' => 'pre_job_status',
'field' => 'slug',
'terms' => array( 'repeat' )
),
),
));
I am creating a simple Query that should query through a meta value containing a number. 1 should be first, 2 should be 2nd and 3 should be 3rd.
For some reason, it comes out like 1, 3, 2 in my query. What am I missing??
$args = array(
'post_type' => 'x-portfolio',
'posts_per_page' => $count,
'paged' => $paged,
'orderBy' => 'meta_value_num',
'meta_key' => 'liste_nr',
'order' => 'asc',
'tax_query' => array(
array(
'taxonomy' => 'portfolio-category',
'field' => 'term_id',
'terms' => $filters,
),
array(
'taxonomy' => 'portfolio-category',
'field' => 'name',
'terms' => 'Accessories',
'operator' => 'NOT IN'
)
)
);
I've run into this before when a CPT, plugin etc has done a custom query and they never reset and it can completely override yours.
Try dropping wp_reset_query() before it and make sure you are using it after.
https://developer.wordpress.org/reference/functions/wp_reset_query/
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 series of loops spitting out 'event' custom post types that I want to only pull posts with a post_status of 'publish'. I've added the post_status of 'publish' in my wp_query array but it doesn't seem to work. I still have scheduled posts showing up.
<?php
$args_hotel_feature = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 2,
'meta_key' => '_expiration_date',
'orderby' => 'meta_value',
'order' => 'ASC',
array(
'key' => '_expiration_date',
'value' => date("m-d-Y"),
'compare' => '>=',
'type' => 'NUMERIC,'
),
'tax_query' => array(
array(
'taxonomy' => 'EventCategory',
'terms' => 'hotel-feature',
'field' => 'slug',
)
),
);
$wp_query4 = new WP_Query($args_hotel_feature);
if($wp_query4->have_posts()) :
while($wp_query4->have_posts()) :
$wp_query4->the_post();
?>
Anyone else experienced this issue?
Use meta_query
$args_hotel_feature = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 2,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_expiration_date',
'value' => date("m-d-Y"),
'compare' => '>=',
'type' => 'NUMERIC,'
)
),
'tax_query' => array(
array(
'taxonomy' => 'EventCategory',
'terms' => 'hotel-feature',
'field' => 'slug',
)
),
);
Turns out the query was fine, however, the post expiration plug-in "Posts Expiration Date" breaks post_staus. Use "Post Expirator" instead.
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 );