1

I have a post ID stored in a variable from my first query and I'd like to use that to highlight the post with the same ID in my second query, but it's not working.

First loop:

        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

            <?php foreach(get_field('relationship') as $post_object): ?>
            <?php $current = get_the_ID($post_object->ID); ?>
            <?php echo $current; ?>
                <a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID) ?></a>
            <?php endforeach; ?>

            <?php the_content();?>

        <?php endwhile; else: ?> 
        <?php endif; ?>

The post is queried in my second loop,(not related to the first), only I don't know how to identify it by the variable from the first loop.

Second:

                <?php $news_arg=array(
                    'post_type' => array ('books'),
                    'post_status' => array( 'publish'),
                    'posts_per_page' => 20,
                    );
                    $arg_query = new WP_Query();
                    $arg_query->query( $arg );
                ?>
                <?php if( $arg_query->have_posts() ) : ?>
                <?php while( $arg_query->have_posts() ) : $arg_query->the_post(); ?>
                <?php print_r($arg_query) ?>
          <li>
            <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
            </li>
                <?php endwhile; ?>
                <?php else : ?>
                <?php endif; ?> 

Thanks.

EDIT: Alex's answer with Chip's correction should work but for some strange reason it doesn't, so I post my full code in case anyone spot the problem. (There are no other queries or any other php on the page):

EDIT: replaced get_the_ID with get_permalink and now it works:

<div class="span8 single_c">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<div class="post-header">
    <h2 class="verseny">
    <a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID) ?></a>
    </h2>
</div>

<?php foreach(get_field('relation') as $post_object): ?>
<?php $current = get_permalink($post_object->ID); ?>
<?php endforeach; ?>
<div class="well single_p">
<?php echo $current ?>
<?php if(get_field('lead')) {
    echo '<p><strong>' . get_field('lead') . '</strong></p>';
} else { } ?>
    <?php the_content();?>

<?php endwhile; else: ?> 
<?php endif; ?>
</div>
</div>      

<div class="span4 single_c">
<div class="well db">
        <ul class="nav nav-list">
    <?php $arg=array(
    'post_type' => array ('books'),
    'post_status' => array( 'publish'),
    'posts_per_page' => 10
    );
    $arg_query = new WP_Query();
    $arg_query->query( $arg );
    ?>
    <?php if( $arg_query->have_posts() ) : ?>
    <?php while( $arg_query->have_posts() ) : $arg_query->the_post(); ?>

>" rel="bookmark" title="">

    <?php endwhile; else : ?>
    <?php endif; ?> 
    </ul>
</div>  
</div>

1 Answer 1

1

The primary loop query post ID is stored in $current. This variable is equivalent to $post->ID. The secondary loop query post ID is available within the loop as $post->ID. Thus, you just need a simple if statement in side your secondary loop:

<?php 
if ( $current == $post->ID ) {
    // This post is the same as the
    // primary loop's current post;
    // do something
}
?>

For example, to add a "current-article" class, I'd try the following on the <li>

<li<?php if ($post->ID == $current) echo 'class="current-article"'; ?>><!-- stuff here --></li>
3
  • Thanks for the edit. I sometimes have a hard time saying what's in my head. I guess that's the bane of developers, right?
    – Alex Lane
    Commented May 17, 2012 at 15:27
  • Thanks. Although echoing the ID within the second loop prints the correct ID, the list item doesn't get the desired class. Even if I just try to use your first snippet for testing, it doesn't work. Also if I try to use post__not_in with $current with the second query, the post gets listed as if nothing happened, however seeing the resulting array with print_r the exluded ID is clearly shown...
    – elbatron
    Commented May 17, 2012 at 15:42
  • I replaced get_the_ID($post_object->ID) with get_permalink($post_object->ID) and it works nice. Still, it's weird that the code with get_the_ID didn't act as expected.
    – elbatron
    Commented May 18, 2012 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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