WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to compare the permalink with a url and then output stuff depending if they match. At the moment I have the code below. On the page it's visibly showing that the permalink and the url (i.e $company) match yet it's outputting "don't show news articles".

Where am I going wrong?

<p>permalink of current page: <?php $link = get_permalink(); echo $link; ?></p>
<?php $portfolioloop = new WP_Query( array( 'post_type' => 'news' ) ); ?>
    <?php while ( $portfolioloop->have_posts() ) : $portfolioloop->the_post(); ?>                           
        <?php while(the_repeater_field('featured_companies')): ?>
            <p>url of linked page: <?php $company = the_sub_field('featured_company'); ?></p>
            <?php  if ($link == $company) { ?>
                show news articles
            <?php } else { ?>
                don't show news articles
            <?php } ?>
        <?php endwhile; ?>
    <?php endwhile; // end of the loop. ?>
share|improve this question

You need to be using get_sub_field(), not the_sub_field() to set $company. the_sub_field() has no return, it just echos the value, whereas get_sub_field() returns the value (but does not echo it).

share|improve this answer
up vote 0 down vote accepted

I think my brain nearly exploded with this, solved the problem using the relationship field and lot's of loops:

<h2>Latest News</h2>    
<?php $link = get_the_title(); ?>
<?php $portfolioloop = new WP_Query( array( 'post_type' => 'news' ) ); ?>
<?php while ( $portfolioloop->have_posts() ) : $portfolioloop->the_post(); ?>  
    <?php $post_link = get_post_permalink(); ?>                        
        <?php  if (get_field('featured_companies') != "") { ?>
            <?php foreach(get_field('featured_companies') as $post): ?>
                <?php $company = get_the_title($post_object->ID); ?>
                <?php if ($company == $link) { ?>
                    <a href="<?php echo $post_link; ?>">News item 1</a>
                <?php } ?> 
            <?php endforeach;?>
        <?php } ?>
<?php endwhile; ?>
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.