Support

Account

Home Forums Front-end Issues Nesting relationship querys

Solved

Nesting relationship querys

  • Hi Guys,

    I am creating a site where their is events , speakers and videos. so the user can create a event add speakers to it with the relationship field and then create a video specific to that event and speaker.

    This is what is have on my events page atm.

    <?php query_posts(array( 'post_type' => 'events') );?>
    			
    			<?php while (have_posts()) : the_post();?>
    			<div class="event-wrapper">
    				<h1><?php the_title();?></h1>
    				<div class="row event-wrap">
    					<div class="span_4 event-info">
    						<h2>Event information</h2>
    						<img src="<?php the_field('image_of_map'); ?>" />
    						<?php the_field('date_of_event');?>
    
    						<?php the_field('event_location_');?>
    			
    						<button class="buttons">Become a speaker</button>
    						<button class="buttons">Book Tickets</button>
    					</div>
    					
    					<div class="span_8 event-speakers">
    						<h2>Speakers</h2>
    						
    						<div class="row">
    							<?php 
    							$posts = get_field('speakers'); 
    							if( $posts ): ?>
    							<?php foreach( $posts as $post): ?>
    							<?php setup_postdata($post); ?>
    							<div class="span_6 speaker">
    								<div class="span_5">
    									<img src="<?php the_field('speakers_image'); ?>" />
    								</div>
    								
    								<div class="span_7">
    									<h3><?php the_title();?></h3>
    									<a href="#">View Profile</a>
    								</div>
    							</div>
    							
    							<?php endforeach; ?>
    							<?php wp_reset_postdata(); ?>
    							<?php endif; ?>
    							</div>
    					</div>
    				</div>
    			</div>
    			<?php endwhile;?>

    I basically need to nest another loop inside so for each speaker it gets their video for that event. Any help would be much appreciated.

    • Elliot

    • November 5, 2013 at 3:04 pm

    Hi @nathanrobjohn

    Can you please provide 3 screenshots to show each post_type’s edit screen?
    This will demonstrate which post types have the relationship fields and then I can advise you on how you can add the query.

    Thanks
    E

  • http://i43.tinypic.com/105p7ht.png
    http://i39.tinypic.com/egs85e.png
    http://i44.tinypic.com/fdzonb.png

    Thank you for your reply please take a look at the images above

  • Also i have changed my speakers to be able to have a repeater field as the topic , so if they speak more than once they can add a topic for each event they spoke at. i was wondering how i would query that also?

  • http://pastebin.com/9UzpCfmx

    Thats my code, so each event should list all speakers and info and then on a speaker it will show their video for that event and the topic.

    Any help would really be appreciated.

  • I think i have the same issue. I have for example a custom post type “task” and a other name “Project”. In “task” i have a ACF relationship name “assign_project” where i select the “project i want. Now, i want to show in my project details template all the task that are assign to this project.

    How can i display that?

  • 									<?php $videos = get_posts(array(
    									  'post_type' => 'videos',
    									  'meta_query' => array(
    										'relation' => 'AND',
    										 array(
    										   'key' => 'speaker', 
    										   'value' => get_the_ID()
    										 ),
    										 array(
    										   'key' => 'events', 
    										   'value' => $eventid
    										 )
    									   )
    									)); 
    									?>
    									
    									<?php $videoquerys = new WP_Query($videos); ?>
    						

    I cam up with trying this inside the foreach(speaker) but i still can’t get the information to show would really appreciate help

  • This reply has been marked as private.
    • Elliot

    • November 9, 2013 at 9:53 am

    Hi @nathanrobjohn

    I think my previous comment didn’t post! Sorry about that.

    Your query is nearly there. Just check out this doc:
    http://www.advancedcustomfields.com/resources/tutorials/querying-relationship-fields/

    You need to make a few modifications to add the LIKE query work. Also, updating to ACF 4.3.0 will ensure that the data is saved in this LIKE friendly format (it is possible that 4.2.2 did not)

    Basically, within your loop foreach speaker, you can perform your get_posts query asking for all videos which have selected the current speaker ID and the event ID.
    Your code already shows you have figured out this logic.

    Something that I can see causing an issue is the setup_postdata function. This is a great function for a single loop, however once you proceed into the nested loop (video), this function will cause all sorts of issues. Basically, it will kill the speakers loop completely!

    I would instead, not use the setup_postdata function, but just pass the current ID into all the WP functions. Also, you will need to change your loop to not use the $post variable, instead use $speaker or $event. You can get a title like so:

    
    echo get_the_title( $speaker->ID );
    

    Hope that helps.

    Thanks
    E

  • Hi Elliot thank you for the reply. im not with the project yet but i just rewrote some of it. would this work do you think?

    <?php
    /** Template Name: Events Page **/
    ?>
    <?php Utilities::get_template_parts( array( 'parts/shared/html-header', 'parts/shared/header' ) ); ?>
    <div class="wrapper">
     
     
     
    <?php query_posts(array( 'post_type' => 'events','meta_key' => 'date','orderby' => 'meta_value_num','order' => 'DESC' ) );?>
                           
                            <?php while (have_posts()) : the_post();?>
                            <?php $date = DateTime::createFromFormat('Ymd', get_field('date'));?>
                            <?php $currentdate = new DateTime();?>
                            <?php if ($date > $currentdate) { ?>
                            <div class="event-wrapper">
                                    <h1>Upcoming Event : <?php the_title();?></h1>
                                    <div class="row event-wrap">
                                            <div class="span_4 event-info">
                                                    <h2>Event information</h2>
                                                   
                                                   
                                                   
                                                    <img />" />
                                                    <p><?php echo $date->format('l jS F Y'); ?> , <?php the_field('time');?></p>
                                                    <p><?php the_field('event_location_');?></p>                   
                                                    <button class="buttons">Become a speaker</button>
                                                    <button class="buttons">Book Tickets</button>
                                            </div>
                                           
                                            <div class="span_8 event-speakers">
                                                    <h2>Speakers</h2>
                                                   
                                                    <div class="row">
                                                            <?php
                                                            $speakers = get_field('speakers');
                                                            if( $speakers ): ?>
                                                            <?php foreach( $speakers as $speaker): ?>
                                                            <div class="span_6 speaker">
                                                                    <div class="span_5">
                                                                            <img />" />
                                                                           
                                                                    </div>
                                                                   
                                                                    <div class="span_7">
                                                                            <h3><?php the_title();?></h3>
                                                                           
                                                                           <?php
    																			$args = array(
    																			  'post_type' => 'videos',
    																			  'meta_query' => array(
    																				'relation' => 'AND',
    																				array(
    																				 'key' => 'speaker',
    																				 'value' => get_the_ID()
    																				),
    																				array(
    																				 'key' => 'events',
    																				 'value' => $eventid
    																				)
    																			  )
    																			);
    
    																			$videos = new WP_Query($args);
    
    																			if($videos->have_posts()) : 
    																			   while($videos->have_posts()) : 
    																				  $videos->the_post();
    
    																			?>
    
    																			<!-- STUFF FOR EACH VIDEO HERE -->
    																			<?php the_field('youtube_videos');?>
    
    																			<?php
    
    																			endwhile;
    																			endif;
    
    																			?>
                                                                    </div>
                                                            </div>
                                                           
                                                            <?php endforeach; ?>
                                                            <?php wp_reset_postdata(); ?>
                                                            <?php endif; ?>
                                                            </div>
                                            </div>
                                    </div>
                            </div>
                            <?php } ?>
                            <?php endwhile;?>
    </div>                 
     
    <?php Utilities::get_template_parts( array( 'parts/shared/footer','parts/shared/html-footer' ) ); ?>
    • Elliot

    • November 10, 2013 at 3:10 pm

    Hi @nathanrobjohn

    There are a few issues above:

    1. The indentation is all over the place and very hard to read. Can you please edit and fix this? Easiest way is to first copy it into a new file and set the indentation, then copy it into the comment box here.
    2. < ?php the_title();?> won’t work within the $speakers loop. You need to pass it the $speaker->ID
    3. get_the_ID() won’t work, you need to use $speaker->ID
    4. Your meta args don’t use the LIKE compare, and are also missing the quots around the ID value. This is explained in the relaitonship foc I mentioned

    Thanks
    E

  • Hi Elliot thanks for your replys and help on this i have edited the query to what you have said.
    http://pastebin.com/T8bV9JVB

    • Elliot

    • November 11, 2013 at 3:53 pm

    Hi @nathanrobjohn

    ok. Is this to show for future readers, or do you have a question?

    Thanks
    E

  • I am printing my query out and this is what its selecting in the db
    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
    INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) WHERE 1=1 AND wp_posts.post_type = ‘videos’ AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_status = ‘private’) AND ( (wp_postmeta.meta_key = ‘speaker’ AND CAST(wp_postmeta.meta_value AS CHAR) = ‘161’)
    AND (mt1.meta_key = ‘event’ AND CAST(mt1.meta_value AS CHAR) = ’43’) ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

    but i cannot get anything to print

    • Elliot

    • November 12, 2013 at 4:31 pm

    Hi @nathanrobjohn

    Looking over your code, you have written your meta malue like this:
    '$speaker->ID'

    This is not correct. It shoudld be:
    '"' . '$speaker->ID' . '"'

    There is a big difference and you can see it from the documentation on querying relationship fields.

    Hope that helps.

    Thanks
    E

  • Got the code working in the end this is the finished piece

    http://hastebin.com/dufucamaja.xml

    • dworth

    • October 24, 2014 at 6:45 pm

    I have the same requirement as nathanrobjohn. Unfortunately, the link where the working code was sitting is no longer active. Is there any chance it could be reposted?

    Many thanks

Viewing 17 posts - 1 through 17 (of 17 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.