Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've set up this query in Wordpress to find all associated news for a company:

    <h2 class="heading">Related 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 $post_title = get_the_title(); ?>                        
    <?php  if (get_field('featured_companies') != "") { ?>
        <p style="margin:0px!IMPORTANT;">
        <?php foreach(get_field('featured_companies') as $post): ?>
            <?php $company = get_the_title(); ?>
            <?php if ($company == $link) { ?>
                <a href="<?php echo $post_link; ?>"><?php echo $post_title; ?></a><br />
            <?php } ?> 
        <?php endforeach;?>
        </p>
    <?php } ?>
<?php endwhile; wp_reset_query(); ?>

I then wanted to create the same thing but find all events associated with the company. Even though news and events are setup exactly the same way it doesn't seem to work, what am I missing???

    <h2 class="heading">Related Events</h2>
    <?php $link_e = get_the_title(); ?> 
    <?php $portfolioloop_e = new WP_Query( array( 'post_type' => 'events' ) ); ?>
<?php while ( $portfolioloop_e->have_posts() ) : $portfolioloop_e->the_post(); ?>  
    <?php $post_link_e = get_post_permalink(); ?>
    <?php $post_title_e = get_the_title(); ?>                        
    <?php  if (get_field('featured_companies') != "") { ?>
        <p style="margin:0px!IMPORTANT;">
        <?php foreach(get_field('featured_companies') as $post_e): ?>
            <?php $company_e = get_the_title(); ?>
            <?php if ($company_e == $link_e) { ?>
                <a href="<?php echo $post_link_e; ?>"><?php echo $post_title_e; ?></a><br />
            <?php } ?> 
        <?php endforeach;?>
        </p>
    <?php } ?>
<?php endwhile; wp_reset_query(); ?>

I tried wp_reset_query, but I don't have any real clue what to do!

share|improve this question
    
What do you get if you print_r() $portfolioloop and $portfolioloop_e? Are you actually managing to get posts on both queries? –  m0r7if3r Feb 28 '12 at 13:56
    
@m0r7if3r The first block of code works great. The second doesn't - I've tried echoing stuff throughout the loops which works until I put it within the $company_e == $link_e as the titles are different, not sure how they're different though. –  Rob Feb 28 '12 at 14:06
add comment

2 Answers

Because you're calling get_post_permalink etc, I think you may need to add wp_reset_postdata(); as well, per this section in the codex:

http://codex.wordpress.org/Class_Reference/WP_Query#Interacting_with_WP_Query

Hope this helps, best of luck!

share|improve this answer
    
Thanks, thought it may be something like but it didn't seem to work. I put it after both the wp_reset_query's. –  Rob Feb 28 '12 at 14:09
add comment
up vote 0 down vote accepted

I added $post_e to this line and it worked:

<?php $company_e = get_the_title($post_e); ?>

Thanks for both your help, it triggered some ideas.

share|improve this answer
add comment

Your Answer

 
discard

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.