Support

Account

Home Forums General Issues Relationship Object Not Showing

Solved

Relationship Object Not Showing

  • Hello,

    I created a custom post type (Events) and I created another custom post type (Instructors), which I classified the instructors post type as a relationship custom field in ACF. I am populating the Event Post Type list just fine, however when I try to call the Instructor custom field, it either wont show up or it will kill the string of events and just show one. Don’t know what I am doing wrong. Thanks in advance for any help you guys can provide. 🙂

    <?php
    /**
    * Template Name: Classes
     *
     */
    get_header(); ?>
    
    <div class="container">
      <?php 
    $today = date('Ymd');
    $instructor = get_field('instructors');
    // get posts
    $posts = get_posts(array(
    	'post_type'			=> 'event',
    	'posts_per_page'	=> -1,
    	'meta_key'			=> 'event_date',
    	'orderby'			=> 'meta_value_num',
    	'order'				=> 'ASC',
      	'meta_query'		=> array(
    	  		array(
    			   'key' => 'event_date',
                  'compare' => '>',
    			  'value' => $today,
                  'type' => 'DATE',
    	'meta_query'		=> array(
    	  		array(
    			  'key'	=> 'instructors',
    			  'value'	=> $instructor,
    			  'compare'	=>'IN',
    ),)			  
    ),)
      	 
    ));
    
    if( $posts ): ?>
    		
    	<?php foreach( $posts as $post ): 
    		
    		setup_postdata( $post )
    		
    		?>
    
      <a class"class"href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
    
    		  <img style="float: right; max-width: 28%;"src="<?php the_field('event_image'); ?>"/><h3>Event Date : 
    <?php the_field('event_date'); ?>
      </h3>
      <h4>Start Time: <?php the_field('start_time'); ?></h4>
      <h4>End Time: <?php the_field('end_time'); ?></h4>
      <p><?php the_field('description'); ?></p>
      <?php if($instructor): ?>
      <?php the_field('instructors'); ?>
    <?php endif; ?>
    <?php echo do_shortcode('[addtoany]'); ?>
      <div class="clear"></div>
      <hr>
    
    <?php endforeach; ?>
      
      
      <?php endif; ?>
    
    	<?php wp_reset_postdata(); ?> 
    
      <div class="clear"></div>
    
      </div>
    <?php get_footer(); ?>
  • Your meta_query is a bit off I think, the second meta_query is overriding the second.

    
    $posts = get_posts(
        array(
            'post_type' => 'event',
            'posts_per_page' => -1,
            'meta_key' => 'event_date',
            'orderby' => 'meta_value_num',
            'order' => 'ASC',
            'meta_query' => array(
                array(
                    'key' => 'event_date',
                    'compare' => '>',
                    'value' => $today,
                    'type' => 'DATE',
                ),
                array(
                    'key' => 'instructors',
                    'value' => $instructor,
                    'compare' =>'IN',
                ),
            )
        ),
    );
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Relationship Object Not Showing’ is closed to new replies.