Sorting query ASC giving wrong order - php

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/

Related

Add filter to wordpress query

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

Wordpress tax query not working with 3 arguments

I have following QUERY:
$query_args = array(
'post_status' => 'publish',
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array(17, 21, 18),
'operator' => 'AND'
)
)
);
When I put 2 arguments in the array it is working but when I put 3 arguments in the array it is not working.
I can't find the answer anywhere, can someone help me please?

Can I do a WP Query within X days of today's date

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

orderby and order filter in get_posts or WP_query function in wordpress not working

I have function in wordpress plugin which queries the posts using get_posts($array). But I wanted this to orderby post_modified field of the posts table in DESCENDING order, for which I have this code below:
$arrPostDtls = get_posts(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1,
'post_status' => array('publish', 'pending', 'trash','draft','auto-draft') ,
'orderby' => 'post_modified',
'order' => 'DESC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
Here, I did implemented the orderby or order clauses to get it sorted accordingly, but it doesnt work. Please suggest or help me to get the sorting as I am willing to.
UPDATE To get the things other way, I used the WP_query method to get the posts. for which I implemented below code:
$arrPostDtls = new WP_query(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1, //unlikely high
'post_status' => array('publish', 'pending', 'trash','draft','auto-draft'),
'orderby' => 'modified',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
From this I recieved the result which also contains the SQL query, and executing the SQL query in PHPmyadmin, I found the exepected result, but when i iterated the "$arrPostDtls->posts", it still gives me the old results.. Please suggest what is wrong here..
Try
get_posts(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1,
'post_status' => array('publish', 'pending',
'trash','draft','auto-draft') ,
'orderby' => array('post_modified' => 'DESC'),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
Update after you update the question
$arrPostDtls = new WP_query(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1, //unlikely high
'post_status' => array('publish', 'pending', 'trash','draft','auto-draft'),
'orderby' => array('modified' => 'DESC'),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
I found my problem on the UI side (thanks to #Zhilevan) where by ajax response was being exposed to a jQuery library DataTable(), whose default ordering was sorting in alphabetical order. I set the ordering parameter to false as:
$("#someid").DataTable({"ordering":false});
And My results were displayed as I was willing to

WP_Query tax_query multiple taxonomies and terms

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

Categories