Simple(I guess) loop issue. Wordpress

So I use advanced custom fields and the relationshipt field.

It works fine as long as I set some posts as related, but if i don’t set any posts as related, it outputs all the posts from the post-type.

Can anyone check my loop and see what the problem might be? Probably easy but considering i am a noob at this…

			<?php
				$ids = get_field('relation', false, false);
				$args = array(

					'post_type' => array('posts', 'sidebar-posts'),
					'post__in' => $ids

				); 
				$the_query = new WP_Query( $args );
			?>
			<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

Try something like this

    <?php
    				$ids = get_field('relation', false, false);
                                    if($ids){
    				        $args = array(
    					    'post_type' => array('posts', 'sidebar-posts'),
    					    'post__in' => $ids
    				        ); 
    				        $the_query = new WP_Query( $args );

                                        if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); 
                                    }

?>

Not sure if that’d work or not, but makes sense to me anyway :smile:

Well it is doing something:P Using that it shows three posts instead of all of them:P

lol

But you still haven’t actually added the field “relation” to any of them, and it’s showing three?

Oh, I see. If you only want it to show when there’s those custom fields and nothing otherwise, you’ll probably want to extend that conditional a line farther. Editing my post to indicate…

Yeah the three posts was the same as a previous loop.

Yeah the problem is it showing posts even tho they arent related to the post ur viewing. The loops is on a single-page. And it show related posts. But if no posts are set as related, it outputs all of the posts.

Try that ^ and see if that helps. If not, you might try posting the rest of the template page code if you can… sort of shooting in the dark a bit, the context might help.

Did you miss wrapping the code in “code tags” , cant see anything=/

I just edited my original response code

thanks, doesnt seem to work do, my page breaks=/

This is the whole thing that comes after the loop that loops out the single-post. What if I were to wrap all of this in the conditional?

    <?php
	$ids = get_field('relation', false, false);
	$args = array(

		'post_type' => array('posts', 'sidebar-posts'),
		'posts_per_page' => 3,
		'post__in' => $ids

);
	$the_query = new WP_Query( $args );
	?>
	<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>	
		<article class="medium-4 columns end">
						<div class="img-wrapper">
							<?php
								if(has_post_thumbnail()) {
								    the_post_thumbnail('mycustomsize');
								} else if(get_field('video')){ 
									the_field('video');
								} else {
									echo '<img src="'.get_bloginfo("template_url").'/img/placeholder.jpg" />';
								}
							?>
						</div>
						<a href="<?php if(get_field('extern')){ echo the_field('extern'); } else { the_permalink(); } ?>">
						</a>
		</article>
	<?php endwhile; endif; wp_reset_query(); ?>

Yeah. That entire thing is its own conditional based on what you have above it, so wrap the whole thing and see what happens. Can’t hurt.

So i rewrote the entire thing like this, wrapper in an if conditonal, but it breaks the site. Can you see what I have done wrong in this one? Im a noob at php so.

<?php
	$ids = get_field('relation', false, false);
	if($ids){
		$args = array(

		'post_type' => array('posts', 'sidebar-posts'),
		'posts_per_page' => 3,
		'post__in' => $ids

		);
		$the_query = new WP_Query( $args );	
		if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();

		echo 'article class="medium-4 columns end">';
		echo '<div class="img-wrapper">';
				
			if(has_post_thumbnail()) {
			    the_post_thumbnail('mycustomsize');
				} else if(get_field('video')){ 
					the_field('video');
				} else {
					echo '<img src="'.get_bloginfo("template_url").'/img/placeholder.jpg" />';
				}

		echo '</div>';
		echo '<a href="' . if(get_field('extern')){ the_field('extern'); } else { the_permalink(); } . '">';
		echo '<h3>' .  if(get_field('related-rubrik')){ the_field('related-rubrik'); } else { the_title(); } . '</h3>';
				
			if(get_field('related-short')){ the_field('related-short'); } else { the_field('short-info'); }
				
		echo '</a>';
		echo '</article>';

		endwhile; endif; wp_reset_query(); 

	}
?>

Right after $ids is defined can you do like a

die(print_r($ids)); 

just for kicks to see what is in that, if anything?

Operating on the assumption that there are 0 posts with the ‘relation’ tag.

You mean like this?

$ids = get_field(‘relation’, false, false);
die(print_r($ids));

Looks like the site is still broken. You don’t see anything else? I mean… it should work wrapping it in a if-statement shouldnt it?

No, that’s not a fix, I just wanted to see what that outputs when you put that in, and view the page. It should output the contents of $die and let us see if it has anything in it or not.

I checked my php here:

http://writecodeonline.com/php/

And i get: Parse error: syntax error, unexpected T_IF on line 27

Whats T_IF?=)

What’s at line 27? And the line(s) before it?

Do you have anywhere (pastebin? something?) where you could just paste the entire php file? I’m afraid there’s an issue outside of what we’re looking at here…

Yeah you can check and edit here, its the loop, if you don’t see anything wrong there I can show the rest of the page

http://www.codeshare.io/AteLH

Yeah that’s fantastic, go ahead and put the rest in there if you don’t mind. Way easier to read that way when dealing with huge chunks :smile:

this is the full single-page

http://www.codeshare.io/AteLH

I tried removing the if conditonal and when i remove that one the site isnt broken anymore, only back to the original problem. So when wrapping the loop in if(){} it breaks.