Home › Forums › Front-end Issues › Conditional Tags Don't work in Relationship Field Query
Hi there!
I’m using a relationship field on my home page to load various different post types as featured content. The 3 post typoes in question are ‘Artist’, ‘Exhibitions’, and ‘Products’.
Each post type has different ACF fields that need to be displayed and so I’m trying to use conditional statements in order to display these fields on the relevant post object. The relationship field is set to return the post object.
Here is an example piece of code:
<?php
$posts = get_field('feature_content');
if( $posts ): ?>
<?php foreach( $posts as $post): // IMPORTANT must be called $post ?>
<?php
setup_postdata($post);
// Show Only for exhibitions
$ex_is_solo = get_field('exhibition_is_solo_show');
$ex_date_start = get_field('exhibition_start_date', false, false);
$ex_date_start = new DateTime($ex_date_start);
?>
<div class="c-subtitle">
<?php // Option 1: This works for exhibitions but if i add an else statement for other post types I can only return one other value ; ?>
<?php if( $ex_is_solo ): ?>
<?php if( $ex_date_start ): ?>
<?php echo 'Feature Exhibition'; ?>
<?php echo $ex_date_start->format('j M'); ?>
<?php endif; ?>
<?php endif; ?>
<?php // this returns Bla
if ( is_singular('exhibitions') ) {
echo $ex_date_start->format('j M');
echo ' Feature Exhibition';
} elseif ( is_singular('artist') ) {
echo date('M');
echo ' Featured Artist';
} elseif ( is_singular('product') ) {
echo date('M');
echo ' Featured Item';
}
else {
echo 'Bla';
}
?>
</div>
<?php wp_reset_postdata(); // IMPORTANT ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
I’m guessing the conditional statement will only work within the loop? Or is there something else I’m missing here? Any help to get a solution would be great.
Many thanks in advance!
Did you ever find out how to do this? I’m running into the same issue trying to filter out custom post types in the relationship results
is_sungular()
looks at the main query and not at the current post. You cannot use is_singular()
here and instead you must look at the post object yourself
if ($post->post_type == 'your post type') {
// do something
...
The topic ‘Conditional Tags Don't work in Relationship Field Query’ 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.