Sort wp_query by multiple meta keys (numeric) in specific order - php

I need to sort a query by a specific order and I failed many times on finding the right solution, but nothing helped – so I'm asking for some hints what I'm doing wrong.
Short story: I have a wp_query showing bikes ordered by the cheapest price. It's working so far. But now I want to show some promotional bikes at first before the order by price starts. So the promotional bikes are showing first and then the "normal" loop.
Promotional bikes are tagged by a ACF field called "promotional_bikes".
This is my wp_query:
$args = array(
'post_type' => 'bikes',
'posts_per_page' => -1,
'facetwp' => true,
'post_status' => 'publish',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => 'baseprice_0_rate', // it's a repeater field in acf
);
meta_key = baseprice_0_rate
meta_value = 100 - 1000 // different values because of the prices
meta_key = promotional_bike
meta_value = 1 // because true or false in acf
This was my last try:
$args = array(
'post_type' => 'bike',
'posts_per_page' => -1,
'facetwp' => true,
'post_status' => 'publish',
'meta_query' => array(
'promo_bike' => array(
'key' => 'promotional_bike',
'compare' => 'EXISTS',
),
'cheapest_bikes' => array(
'key' => 'baseprice_0_rate',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'promotional_bike' => 'ASC',
'cheapest_bikes' => 'meta_value_num',
)
);
but I get 0 results and it doesnt work.
Can anybody give me a hint how to first show the promo bikes and then show the bikes (cheap to high) ?
Many thanks :-)

I came across this while trying to figure out how to orderby multiple numeric meta fields. Finally got it figured out and wanted to post some tips in case anyone else arrives here for the same reason.
Assuming the OP's query had returned results, here's how to sort by multiple numeric meta fields. The key is the type attribute when defining the meta_query clauses.
$args = [
'post_type' => 'bike',
'posts_per_page' => -1,
'facetwp' => true,
'post_status' => 'publish',
'meta_query' => [
'promo_bike' => [
'key' => 'promotional_bike',
'compare' => 'EXISTS',
'type' => 'NUMERIC', // makes orderby sort numerically instead of alphabetically
],
'cheapest_bikes' => [
'key' => 'baseprice_0_rate',
'compare' => 'EXISTS',
'type' => 'NUMERIC',
],
],
'orderby' => [
'promo_bike' => 'DESC', // assuming you want promotional_bike=1 at the top
'cheapest_bikes' => 'ASC',
],
]
Sources:
https://ideone.com/RUoyZs
https://wordpress.stackexchange.com/questions/246355/order-by-multiple-meta-key-and-meta-value

you don't need to code for that. simply you can short your product from Category>Count and then you can drag & drop... for better understanding - you can see my video tutorial.

Related

Question About Multiple Sorting in Word Press

I have a page that shows standings for an online Fifa league, the standings should be sorted by Points and then Goal Differential. I collect the data for Wins, Draws, Losses, Goals For and Goals against. Here is my code -
<?php
$args = array(
'post_type' => 'post',
'cat' => '5',
'numberposts' => 12,
'meta_query' => array(
'relation' => 'AND',
'_fifa_w' => array(
'key' => '_fifa_w',
),
'_fifa_d' => array(
'key' => '_fifa_d',
),
'_fifa_gf' => array(
'key' => '_fifa_gf',
),
'_fifa_ga' => array(
'key' => '_fifa_ga',
),
),
'orderby' => array(
'_fifa_w' => 'DESC',
'_fifa_d' => 'DESC',
'_fifa_gf' => 'ASC',
'_fifa_ga' => 'DESC'
)
);
$product_posts = get_posts( $args );
?>
This sorts by total points but does nothing for the goal differential. If I exclude _fifa_w (wins) and _fifa_d (draws) from the code like this -
'orderby' => array(
'_fifa_gf' => 'ASC',
'_fifa_ga' => 'DESC'
)
);
Then it sorts by goal differential properly.
How can I get it to all work togetheR?
Try:
'orderby' => '_fifa_w _fifa_d _fifa_gf-_fifa_ga',
'order' => 'DESC'
I don't use Wordpress, but the idea is to sort by the first two columns normally, then sort on the calculated difference between the goalsfor and goals against.
Probably a good idea not to blindly trust me, but to eyeball the rendered query to be sure that Wordpress isn't backtick-wrapping the mathematical expression. Maybe $wp_query->request will show you what is being rendered.
Otherwise, there is always the php option of usort() on your result set.

Wordpress sort by number custom fields gives different results

I have a query which gives me custom post type posts which are sorted by category and a custom fields which has a number that indicates the amount of votes for the post. The problem is, if 2 posts have 3 votes, sometimes when you refresh, it changes their position. Here is the code:
$args = array(
'numberposts' => 10,
'post_type' => 'nominees',
'meta_query' => array(
array(
'key' => 'category',
'value' => get_the_ID(),
'compare' => '=',
)
),
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'count_nominees'
);
I tried adding order on the category meta query, but nothing changes, I still sometimes get different results when refreshing, if the posts have same amount of votes. What is the right way to add second order, by ID or something?
Thanks.
As mentioned in the comments, I think this might help, but you might be able to extend it to be a little more specific in the search query for your use case.
$args = array(
'numberposts' => 10,
'post_type' => 'nominees',
'meta_query' => array(
array(
'key' => 'category',
'value' => get_the_ID(),
'compare' => '='
)
),
'meta_key' => 'count_nominees',
'orderby' => array(
'count_nominees' => 'DESC',
'ID' => 'DESC'
)
);
That should get 10 posts in the nominees post type, only if they're part of category xyz, and have post meta of count_nominees and order by count_nominees first in descending order and then by the post ID in descending order.
You can use the WP_Query documentation on the WordPress codex for more information about complex queries using post meta data.

WP Query - Order by metavalue and keep other posts

Can't figure out this problem. Goal: I want to show all posts and order them by a post meta value, but not all posts have that meta key.
Problem: Not all posts are shown, only the ones that have that meta key (and meta value).
$args = array(
'post_type' => 'post',
'meta_key' => 'post_custom_field_1',
'orderby' => 'meta_value',
'order' => 'ASC',
'paged' => $paged,'post_type' => 'post' );
Question: How can I show all posts and sort them by a meta_key? Posts that have the meta_key are shown first and ordered by name and posts that don't have the meta_key follow and are sorted by title.
You'll have to use EXISTS for compare. There are plenty of examples in the docs for WP_Query, you can adapt it to your case:
function orderby_fieldifexists($orderby) {
return "mt1.post_id IS NOT NULL DESC, wp_postmeta.meta_value ASC";
}
add_filter("posts_orderby", "orderby_fieldifexists", 10, 1);
$query = new WP_Query(
array(
'post_type' => 'post',
'meta_query' => array(
"relation" => "or",
'custom_field_value' => array(
'key' => 'post_custom_field_1',
),
'custom_field' => array(
'key' => 'post_custom_field_1',
'compare' => 'NOT EXISTS',
),
),
'orderby' => array(
'custom_field' => 'ASC'
),
'posts_per_page' => 5,
)
);
I've initially had a simpler version in here, but then remembered that it would be silly to sort numerically if some values will be NULL. This version will find all posts, but get the value as well, and sort descending on "does it exist?" and then ascending on the actual values, so a post with a custom field value of -1 will be listed before those without that custom field value.

Wordpress AJAX Load More Orderby Meta_VALUE_NUM

Helo,
I have multiple meta_key value pairs inside my custom posts. So to work around this what I did is that I ran a WP_QUERY initially which creates an array of posts ID which I then pass to load more to load those.
The array that I build using WP_Query is in the correct order. However when I use the IDs in post_in parameter it is not displaying them in the given order. I have also tried to give orderby="meta_value_num" parameter but it doesnt work either.
Here is WP_QUERY which is working perfectly
$loop = new WP_Query(
array(
'post_type' => 'properties',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'listing_type',
'value' => array(3,2),
'type' => 'NUMERIC',
),
array(
'key' => 'payment_status',
'value' => 'yes',
),
array(
'key' => 'expired',
'value' => 'no',
- ),
),
'orderby' => 'meta_value_num',
'meta_key' => 'listing_type',
'order' => 'DESC'
));
Here is the shortcode:
[ajax_load_more post_type="properties" post__in="'.implode(',',$featured).'" posts_per_page="10" scroll="false" transition="fade" button_label="'.$l_more.'" button_loading_label="'.$l_more_2.'" container_type="ul" css_classes="items",orderby="meta_value_num" meta_key="listing_type"]
However it doesnt order posts as it should because $featured array has it in required order. Even if I remove order by and meta_key parameter it doesnt work.
Please Help
Ahmar
First, you have an error in your shortcode.
[ajax_load_more post_type="properties" post__in="'.implode(',',$featured).'" posts_per_page="10" scroll="false" transition="fade" button_label="'.$l_more.'" button_loading_label="'.$l_more_2.'" container_type="ul" css_classes="items" orderby="meta_value_num" meta_key="listing_type"]
Second, you should orderby="post__in" to preserve the post__in ordering.
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

php query based on relational child elements for wordpress loop

I will try and explain this a simple as possible.
I have 3 post types that come into play here.
reports
events (motogp-2013, motogp-2014)
circuits
On my 'events' single.php to display the event information, I am trying to create a query to display related 'reports'.
And the only relation that the event content can have with a report is the circuit.
When I create a 'report', I have to assign an 'event' to it. But when I create a 'event' prior to a report, I have to assign a 'circuit' to it.
For example, it looks like this...
> 'report' - Second place for Pedrosa as black flag terminates race for Marquez (1023)
> 'event' - Australian Grand Prix (662)
> 'circuit' - Phillip Island (156)
So I am trying to query 'reports', which had 'events' at specific 'circuits'.
On my 'events' single.php, I do have the circuit id, but I don't know how to list the reports that which are at that circuit.
I can easily show related 'events' using this query
$related = new WP_Query(array(
'post_type' => array('motogp-2013','motogp-2014'),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_calendar_circuit',
'value' => $circuit,
'compare' => '=',
'type' => 'NUMERIC'
)
)
));
But this is not what I'm trying to achieve, I need to show related reports.
This reports_race_event meta key contains the id number of the 'event', and the 'event' contains the meta key event_calendar_circuit which contains the circuit id.
My question is how do I do this for 'reports', when the only meta_key I have is reports_race_event
I need to list 'reports' which we're held at a specific $circuit
If any one can help me that would be great thanks.
I've figured out how I can do this!
But I still need help please, I can list all related reports like this...
$related_reports = new WP_Query(array(
'post_type' => 'reports',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'reports_race_event',
'value' => $events_at_circuit,
'compare' => not sure,
'type' => not sure
)
)
));
But I need to create an array of 'event' id numbers and store it in this $events_at_circuit variable.
Buy using this query first...
global $circuit;
$related_events = get_posts(array(
'post_type' => array('motogp-2013','motogp-2014'),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_calendar_circuit',
'value' => $circuit,
'compare' => '=',
'type' => 'NUMERIC'
)
)
));
My question is now, how do I get the returned post id numbers from the $related_events query and store them as an array in $events_at_circuit
I eventually got there...
Here is the answer, I had to store the array numbers for the events that where at that circuit, then queried by meta_key value using an array variable. Then comparing using wordpress meta IN comparison. See full code below...
$events_at_circuit = null;
$related_events = get_posts(array(
'post_type' => array('motogp-2013','motogp-2014'),
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_calendar_circuit',
'value' => $circuit,
'compare' => '=',
'type' => 'NUMERIC'
)
)
));
if( !empty( $related_events ) ){ foreach( $related_events as $related_event ) $events_at_circuit[] = $related_event->ID; }
$related_reports = new WP_Query(array(
'post_type' => 'reports',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'reports_race_event',
'value' => $events_at_circuit,
'compare' => 'IN',
'type' => 'NUMERIC'
)
)
));
if ( $related_reports->have_posts() ) :

Categories