php query based on relational child elements for wordpress loop - php

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() ) :

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.

How to get posts filtered by custom field that is post object type?

I want to get posts of an specific post type where those have custom field with type of post type.
I have post type named "tour", and tour post type has custom field named "country" that its type is Post Object.
The query for get all tours that their country is "Turkey":
$located_tours = get_posts(array(
'numberposts' => -1,
'post_type' => 'tour',
'meta_query' => array(
'key' => 'country',
'title' => 'Turkey',
'compare' => 'LIKE'
)
));
But this query return all tours without any filtering on Country
Anyone have any idea?
Thanks
First of all, meta_query expects nested arrays, so it's not:
'meta_query' => array(
'key' => THE_KEY,
'value' => THE_VALUE,
),
but:
'meta_query' => array(
array(
'key' => THE_KEY,
'value' => THE_VALUE,
),
),
Note that compare isn't necessary in your case as its default value is =.
Anyway, you have a conceptual mistake here. If you are using ACF and your country custom field type is Post Object, the value for this field stored in the database is not title but the related post ID (either your Return Format is Post Object or Post ID).
Something like that (in your wp_postmetatable):
So, you need to query by country's ID, not country's Title. You could use get_page_by_title to retrieve your desired country ID:
// Assuming your Country's CPT slug is 'country'
$country = get_page_by_title( 'Turkey', 'OBJECT', 'country' );
$tours_args = array(
'posts_per_page' => -1,
'post_type' => 'tour',
'meta_query' => array(
array(
'key' => 'country',
'value' => $country->ID
)
)
);
$located_tours = get_posts( $tours_args );
Hope this helps!
Try this :
$tour_args = array(
'posts_per_page' => -1,
'post_type' => 'tour',
'meta_query' => array(
array(
'key' => 'country',
'value' => 'Turkey',
'compare' => '=',
),
),
);
$located_tours = new WP_Query($tour_args);

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

WP query filter posts based on url arguments

I have a set of wordpress postst that have various metafields. I'm trying to filter through these posts depending on the arguments given in a url. For example.
www.example.com/?artist=franksinatra&mood=
Should bring up all posts with frank sinatra as the artist field, but ignoring the mood field because its blank. That works fine. But I'd like to narrow my search even further. So when I add additional arguments such as:
www.example.com/?artist=franksinatra&mood=sad
All posts with that field combination should show up. Here's the beginning of my loop that allows me to search using one argument but not additional ones. Thank you.
<?php
$queryArtist = $_GET['artist'];
$queryMood = $_GET['mood'];
$paged = get_query_var('paged');
$loop = new WP_Query( array(
'posts_per_page' => 5,
'post_type' => 'catalog',
'paged' => $paged,
'order_by' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'artist',
'value' => $queryArtist,
'compare' => '=',
)
)
)
);
while ( $loop->have_posts() ) : $loop->the_post(); ?>
you can add additional queries like this:
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'artist',
'value' => $queryArtist,
'compare' => '=',
),
array(
'key' => 'mood',
'value' => $queryMood,
'compare' => '=',
)
)
by switching the relation from "OR" to "AND" both conditions have to be true

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 :)

Categories