Support

Account

Home Forums General Issues relationship field sort order

Helping

relationship field sort order

  • Hi,
    When you create a relationship field in ACF, how do you then use this sort order to display the related on a page?

    This is my code:

    <?php  
    	 $args = array(
    		'post_type' => 'client',
    		'posts_per_page' => '8',
    		'post_status' => 'publish',
    		'orderby' => 'rand',
    		
    	);
    	query_posts($args);
    	while ( have_posts() ) : the_post();
    		 $cliente = get_the_title();
    		 $logo_hover = get_field('logo_hover');
             $logoclient = get_field('logo');
    		 $id = get_the_ID();
    		
    		 $case = get_posts(array(
    			'post_type' => 'casestudy',
    			'meta_query' => array(
    				array(
    					'key' => 'cliente', // name of custom field
    					'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
    					'compare' => 'LIKE'
    				)
    			)
    		));
    	 ?>  
    	<?php if( $case )  ?>
    				<div class="brand">
    				<?php
    				if (count($case) > 0) {
    					foreach( $case as $cliente2 ): 
    						if (count($case) == 1) {
    						?>
    						<a href="<?php echo get_permalink( $cliente2->ID ); ?>">
    							<img class="img-cliente" src="<?php echo $logoclient['url']; ?>" alt="<?php echo $logoclient['alt']; ?>" />
                            <img src="<?php echo $logo_hover['url']; ?>" alt="<?php echo $logo_hover['alt']; ?>" />
    						</a>
    						<?php
    						} else {	
    						?>
    						<a href="<?php echo get_permalink( $case->ID ); ?>">
    							<img class="img-cliente" src="<?php echo $logoclient['url']; ?>" alt="<?php echo $logoclient['alt']; ?>" />
                           	 <img src="<?php echo $logo_hover['url']; ?>" alt="<?php echo $logo_hover['alt']; ?>" />
    						</a>
    						<?php
    						}
    					endforeach;
    				} else {
    				?>	
    					<a href="javascript:void(0)" style="cursor:default;">
    						<img class="img-cliente" src="<?php echo $logoclient['url']; ?>" alt="<?php echo $logoclient['alt']; ?>" />
                            <img src="<?php echo $logo_hover['url']; ?>" alt="<?php echo $logo_hover['alt']; ?>" />
    					</a>
    				<?php
    				} 
    				?>						
     				</div>
        <?php
        endwhile;
    	wp_reset_query();
        ?>
  • Try changing these to lines:

    
    if (count($case) > 0) {
      foreach( $case as $cliente2 ): 
    

    to

    
    if (count($case->posts) > 0) {
      foreach( $case->posts as $cliente2 ): 
    

    I don’t understand what you’re trying to do here

    
    <a href="<?php echo get_permalink( $case->ID ); ?>">
    

    $case in your code is a WP_Query object, so $case->ID has no value.

    To get a better idea of what $case holds you can do this so that you can see everything that’s in it.

    echo '<pre>'; print_r($case); echo '</pre>';

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

The topic ‘relationship field sort order’ is closed to new replies.