Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

php query based on relational child elements for wordpress loop WordPress

  • SOLVED

Please see my question here...

[[LINK href="http://stackoverflow.com/questions/21217063/php-query-based-on-relational-child-elements-for-wordpress-loop"]]http://stackoverflow.com/questions/21217063/php-query-based-on-relational-child-elements-for-wordpress-loop[[/LINK]]

Thanks

Answers (2)

2014-01-19

Arnav Joy answers:

try this , and check if the title of the reports are ok or not

<?php

global $wpdb;

$current_event_post_id = get_the_ID();

$col = $wpdb->get_col("SELECT post_id FROM ".$wpdb->prefix."postmeta WHERE meta_key= 'reports_race_event' AND meta_value = ".$current_event_post_id );

if( !empty( $col ) ){
foreach( $col as $report_post_id ){
echo get_the_title($report_post_id );
echo '<br>';
}
}


Josh Cranwell comments:

Hi Arnav

It's not returning anything.

I've updated my question by the way...


Arnav Joy comments:

try this

if( !empty( $related_events ) ){
foreach( $related_events as $post_arr )
$events_at_circuit[] = $post_arr->ID;
}


Josh Cranwell comments:

Thanks Arave that works.

Can you help me with this bit....

$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 $post_arr ) $events_at_circuit[] = $post_arr->ID; }

echo print_r($events_at_circuit);

$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' => '=',
'type' => 'NUMERIC'
)
)
));


Its the compare bit, 'compare' => '=',

Is it possible to compare using an array in the value?


Josh Cranwell comments:

I've done it.

I use this...

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



Thanks for you help :-)


Arnav Joy comments:

Glad to hear that .

Good Luck!

2014-01-19

Remy answers:

Have you tried to use the posts 2 posts plugin ? [[LINK href="http://wordpress.org/plugins/posts-to-posts/"]]http://wordpress.org/plugins/posts-to-posts/[[/LINK]]

It's really easy to use and could simplify your post types relations a lot


Josh Cranwell comments:

I have firgured out how to do it, sort of, please see updated question


Josh Cranwell comments:

I have firgured out how to do it, sort of, please see updated question