I have created two post types, events and registrations and would like to be able to loop through the events and then fetch the registrations for each event, but WP_Query seems not to be working.
The data is setup for 9 events and 3 registrations, two for one event and one for another
It seems to not fetch the registration data correctly.
$events = new WP_Query(Array(
'posts_per_page' => -1,
'post_type' => 'event'
));
if ($events->have_Posts()){
while($events->have_posts()){
$events->the_post();
echo "<p>Event " . get_the_title() . '</p>';
$eventID = $events->get_the_ID();
echo $eventID;
$args = array(
'post_type' => 'registration',
'meta_key' => 'event_id',
'meta_value' => $eventID,
'meta_compare' => '='
);
$registrations = new WP_Query($args);
while ($registrations->have_posts()){
$registrations->the_post();
echo "<p>The registrations list " . get_the_title() . "</p>";
}
}
}
I get the following results
Event 1
Event 2
Event 3
Event 4
Event 5
Event 6
Event 7
Event 8
Event 9
If I change the line 'meta_compare' => '='
to 'meta_compare' => '!='
I get the following
Event 1
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 2
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 3
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 4
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 5
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 6r Registration
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 7
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 8
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 9
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Not sure why this is proving to be so hard, maybe it's because they're both posts, just different types, from the same table.
Can anyone help please?
I have created two post types, events and registrations and would like to be able to loop through the events and then fetch the registrations for each event, but WP_Query seems not to be working.
The data is setup for 9 events and 3 registrations, two for one event and one for another
It seems to not fetch the registration data correctly.
$events = new WP_Query(Array(
'posts_per_page' => -1,
'post_type' => 'event'
));
if ($events->have_Posts()){
while($events->have_posts()){
$events->the_post();
echo "<p>Event " . get_the_title() . '</p>';
$eventID = $events->get_the_ID();
echo $eventID;
$args = array(
'post_type' => 'registration',
'meta_key' => 'event_id',
'meta_value' => $eventID,
'meta_compare' => '='
);
$registrations = new WP_Query($args);
while ($registrations->have_posts()){
$registrations->the_post();
echo "<p>The registrations list " . get_the_title() . "</p>";
}
}
}
I get the following results
Event 1
Event 2
Event 3
Event 4
Event 5
Event 6
Event 7
Event 8
Event 9
If I change the line 'meta_compare' => '='
to 'meta_compare' => '!='
I get the following
Event 1
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 2
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 3
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 4
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 5
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 6r Registration
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 7
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 8
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Event 9
The registrations list for Evnt 1
The registrations list for Evnt 2
The registrations list for Evnt 1
Not sure why this is proving to be so hard, maybe it's because they're both posts, just different types, from the same table.
Can anyone help please?
Share Improve this question edited Jun 9, 2020 at 0:52 ViralP 3101 silver badge5 bronze badges asked Jun 8, 2020 at 19:09 PeterBPeterB 72 bronze badges2 Answers
Reset to default 0As per WP documentation,
Using the WP_Meta_Query::parse_query_vars( $query ) method: You can use this method, if you want to use the simple query args(
meta_key
,meta_value
,meta_type
,meta_compare
), or if you are unsure of the presence of meta query parameters.
Hence, I tried converting meta_key
, meta_value
and meta_compare
in source code to wrap inside meta_query
as key
, value
, compare
.
Also, added if( $registrations->have_post() ) {
Can you check if below code snippet works for you ?
$events = new WP_Query(Array(
'posts_per_page' => -1,
'post_type' => 'event'
));
if ($events->have_posts()){
while($events->have_posts()){
$events->the_post();
echo "<p>Event " . get_the_title() . '</p>';
$eventID = $events->get_the_ID();
echo $eventID;
$args = array(
'post_type' => 'registration',
'meta_query' => array (
'key' => 'event_id',
'value' => $eventID,
'compare' => '='
)
);
$registrations = new WP_Query($args);
if( $registrations->have_post() ) {
while ($registrations->have_posts()){
$registrations->the_post();
echo "<p>The registrations list " . get_the_title() . "</p>";
}
}
}
}
Edit 1 :
If the event_id
is an ACF then you should try fetching registrations using ACF functions.
Ref: https://www.advancedcustomfields/resources/query-posts-custom-fields/
echo $eventID;
$registrations = get_posts(array(
'numberposts' => -1,
'post_type' => 'registration',
'meta_query' => array(
array(
'key' => 'event_id',
'value' => $eventID,
'compare' => '=',
),
),
));
if( $registrations->have_post() ) {
while ($registrations->have_posts()){
$registrations->the_post();
echo "<p>The registrations list " . get_the_title() . "</p>";
}
}
I have managed to sort this with your help, thanks. I have still used WP_Query but the problem lay in the fact that the event_id is a relationship and therefore an array, so "=" as the meta_type wouldn't work Here is the working code.
$events = new WP_Query(Array(
'posts_per_page' => -1,
'post_type' => 'event',
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
if ($events->have_Posts()){
while($events->have_posts()){
$events->the_post();
echo "<p>Event " . get_the_title() . ' ' . get_the_id() . '</p>';
$registrations = new WP_Query(Array(
'posts_per_page' => -1,
'post_type' => "registration",
'meta_key' => 'boat_type',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key'=> 'event',
'compare' => 'LIKE',
'value' => '"' . get_the_ID() . '"'
)
)
));
if ($registrations->have_posts()){
while ($registrations->have_posts()){
$registrations->the_post();
echo "<p>---------reg title is: " . get_the_title() . " and id is " . get_the_ID() . "</p>";
}
}
}
}
Now displays
Event 1
Event 2
---------reg title is: Manual Registration and id is 221
Event 3
Event 4
---------reg title is: Registration and id is 217
---------reg title is: Another Registration and id is 222
Event 5
Event 6
Event 7
Event 8
Event 9