Support

Account

Home Forums Front-end Issues Relationship query – Get url to linked post Reply To: Relationship query – Get url to linked post

  • Ok, so I’ve used the code in in the tutorial: https://www.advancedcustomfields.com/resources/querying-relationship-fields/#single-location.php and modified it a bit to make it more like I want it:

    <?php $lagenheter = get_field('lagenheter'); ?>
    						<?php if( $lagenheter ): ?>
    				<h4 class="space">Lägenheter med denna planlösning:</h4>
    						<div class="img-overlay-wrap halfsized">
    							
    						<img src="<?php echo get_template_directory_uri(); ?>/img/vaningar_both.svg" />
    							<svg class="flats" viewBox="0 0 200 200">
    							<?php foreach( $lagenheter as $lagenhet ): ?>
    								<?php setup_postdata($lagenhet); ?>
    								
    								
    								<?php 
    
    						/*
    						*  Query posts for a relationship value.
    						*  This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)
    						*/
    								
    								
    
    						$planlosning = get_posts(array(
    							'post_type' => 'lagenhetstyp',
    							'meta_query' => array(
    								array(
    									'key' => 'lagenheter', // name of custom field
    									'value' => $lagenhet->ID, // matches exactly "123", not just 123. This prevents a match for "1234"
    									'compare' => 'LIKE',
    									'posts_per_page' => 1
    								)
    							)
    						));		
    
    						?>

    this doesn’t work. Why is that? Could someone please help me? 🙂