Support

Account

Home Forums Front-end Issues Relationship Field Empty

Solved

Relationship Field Empty

  • Hi,

    I was using the relationship field for a slider. The code is working when selecting posts to show in the slider and if I empty the relationship field, it will display all latest posts in the site.

    I’m also using WP_Query arguments to run the field. All I need now is a code to not display a posts if the field is empty.

    Here’s sample code I used.

    <?php $ids = get_field('featured_posts', 'options', false, false);
    			$query = new WP_Query(array(
    				'post_type'      	=> 'post',
    				'posts_per_page'	=> 5,
    				'post__in'		=> $ids,
    			));
    			if ( $query->have_posts() ): ?>
    			<ul class="slides">
    			<?php while ( $query->have_posts() ) : $query->the_post(); // variable must NOT be called $post (IMPORTANT) ?>
    				<li>loop here</li>
    			<?php endwhile; ?>
    			</ul>
    			<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    		<?php endif; ?>

    Warm regards,
    Dodzki

  • FIXED! A friend help me. Here’s an update.

    <?php if( $ids = get_field('featured_posts', 'options', false, false) ) :
    			$query = new WP_Query(array(
    				'post_type'      	=> 'post',
    				'posts_per_page'	=> 5,
    				'post__in'		=> $ids,
    				'post_status' => 'publish',
    			));
    			if ( $query->have_posts() ) : ?>
    			<div class="">
    				<ul class="slides">
    				<?php while ( $query->have_posts() ) : $query->the_post(); // variable must NOT be called $post (IMPORTANT) ?>
    					<li>Loop here</li>
    				<?php endwhile; wp_reset_postdata(); ?>
    				</ul>
    			</div>
    			<div class="fix"></div>
    			<?php else : ?>
    			<?php endif; endif; ?>
  • Hi @dodzki

    Great!
    Please mark your own post as the answer so this topic is closed properly 🙂

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

The topic ‘Relationship Field Empty’ is closed to new replies.