Question About Multiple Sorting in Word Press - php

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.

Related

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

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.

Need assistance with wp_user_query not working

I need some help getting a wp_user_query to work; been struggling and trying lots of stuff but cannot get it to work proper way.
here is my code:
$args = array(
'meta_query' => array(
'role' => 'personal-injury-lawyer',
'orderby' => 'meta_value',
'meta_key' => 'lawyer_numeric_rank',
'order' => 'ASC',
array(
'key' => 'lawyer_location',
'value' => 'London',
'compare' => '='
),
)
);
$london_user_query = new WP_User_Query($args);
if ( ! empty($london_user_query->results)) {
foreach ( $london_user_query->results as $user ) {
I know the foreach loop is not closed; just trying shorten this...
I am trying to do three things on this query:
Show roles of personal-injury-lawyer
Show where meta_key field of lawyer_location = London
Order by meta_value where meta_key = lawyer_numeric_rank in ASC order
I have tried this a number of ways but cannot get this to work with both filtering on London location and also ordering by the rank...
The code right now does filter on location; but the orderby part is not working; if i remove the location filter from this; then the orderby does work...
I hope someone can help with this.
you were forming the meta query argument incorrectly. The below code should help you with quite a few more meta queries as well.
$args = array(
'role'=> 'personal-injury-lawyer', //assuming you have created a role that corresponds..
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'lawyer_numeric_rank',
'meta_query' => array(
// 'relation'=> 'AND', --> if you want more conditions
array(
'key' => 'lawyer_location',
'value' => 'London',
'compare' => '='
),
/* even more conditions with OR, cumulative effect is match lawyer location AND 1 of the nested arrays
array(
'relation' => 'OR',
array(
'key' => '',
'value' => '',
'compare' => '=',
),
array(
'key' => '',
'value' => '',
'compare' => '=',
),
),
*/
)
);

Wordpress Wp_User_Query order by custom value

I have a custom field in my users field, called order. It's an input field where I can add numbers that indicate the order in which my users should appear in the query. I also have a custom field that indicates a level of a user (Worker, Support, Outside help...)
I have arguments for the query set up like:
$args = array(
'meta_query' => array(
0 => array(
'key' => 'user_select',
'value' => 'worker',
),
1 => array(
'key' => 'order',
)
),
'orderby' => 'order'
);
$user_query = new WP_User_Query( $args );
but this is not working. The worker one is working (I get users assigned only to workers value), but the order is not working. How can I make it so that the query will output users according to numbers (in ascending order).
Found the solution!
$args = array(
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'order',
),
array(
'key' => 'user_select',
'value' => 'worker',
)
),
);
Seems to work :)

What is wrong with wordpress code for displaying random popular posts?

<?php
$args_popular = array(
'post_type' => 'post',
'meta_key'='wpb_post_views_count',
'ignore_sticky_posts' => 1,
'posts_per_page' => $entries_display,
'orderby' => 'meta_value_num' ,
'order' => 'rand()',
array(
'key' => 'wpb_post_views_count',
'value' => '1000',
'compare' => '>=',
),
);
?>
I need to display popular posts whose views >1000 and the posts need to be random. This is the code I have written.
Any suggestions would be appreciated.
You have a lot of issues here
order value is invalid, valid values are ASC and DESC
To order random, orderby needs to be set to rand
You do not need to set the meta_key as you are ordering randomly
You meta_query is incomplete. Your array should be wrappped in another array, and parameter should be meta_query
This should work
$args_popular = array(
'post_type' => 'post',
'ignore_sticky_posts' => 1,
'posts_per_page' => $entries_display,
'orderby' => 'rand',
'meta_query' => array(
array(
'key' => 'wpb_post_views_count',
'value' => '1000',
'compare' => '>=',
'type' => 'decimal',
),
),
);

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