I’ve run into an interesting issue when querying a relationship of posts while displaying posts in a search listing: Once the first query is run and relationship is found – it will display the relationship and then halts and no longer displays the remaining search items.
It’s attempted using both the setup postdata and no post data methods. I’m using a similar query elsewhere in the site without any issues, it appears to be only on search.php pages.
Any help greatly appreciated.
Hi @tanmccuin
Could you please share the search.php file? Also, please make sure that this isn’t caused by other plugins or other functions in your theme by trying to reproduce the issue on one of the WordPress’ stock themes (like Twenty Fifteen) with other plugins deactivated.
Thanks!
It’s very simple standard search.php – i’m fairly certain it isn’t templating related but have a look – it’s built off of Bones WP :
<?php get_header('search'); ?>
<div id="content">
<div id="inner-content" class="wrap cf">
<main id="main" class="cf" role="main" itemscope itemprop="mainContentOfPage" itemtype="http://schema.org/Blog">
<!-- USER PANELS -->
<article class="cf" role="article" itemscope itemtype="http://schema.org/BlogPosting">
<section class="middlecontent cf" itemprop="articleBody">
<h3>Results:</h3>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
$posts = get_field('author_selector');
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endforeach; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<figure class="effect-marley m-all t-1of3 d-1of3">
<div class="thumbwrap">
<?php if ( get_the_post_thumbnail($post_id) != '' ) {
echo '<a href="'; the_permalink(); echo '" class="thumbnail-wrapper">';
the_post_thumbnail('sterling-largethumb');
echo '</a>';
} else {
echo '<a href="'; the_permalink(); echo '" class="thumbnail-wrapper">';
echo '<div class="fillthumb" style="background-image:url(';
echo catch_that_image();
echo ');"></div>';
echo '</a>';
} ?>
</div>
<figcaption>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?>">View more</a>
</figcaption>
</figure>
<?php endwhile; ?>
<?php wpex_pagination(); ?>
<?php else : ?>
<?php endif; ?>
</section>
</article>
</main>
</div>
</div>
<?php get_template_part( 'partials/socialmedia', '' ); ?>
<?php get_footer(); ?>
hmm. I may have solved the issue – tell me if this makes sense, i changed the $posts var to $authors and that seems to solved the conflict, must be $posts is used already?
Hi @tanmccuin
Yes, $post is a global variable used by WordPress. That’s why you need the setup postdata method in the loop. This page should give you more idea about it: https://codex.wordpress.org/Global_Variables.
Hope this helps.
The topic ‘Relationship query breaking search.php results’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.