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 am trying to put a query_posts inside a WP_query.

Anything after my query_posts doesn't work properly. Here is my code. What am I doing wrong?

<?php
    $args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'featured' );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<div id="latest">
    <h2>The Latest Issue</h2>
    <div id="breaking-bg">
        <?php 
        if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
          the_post_thumbnail();
        } 
        ?>  
    </div>
    <div class="breaking">  
        <div id="featured-content">
            <p class="datetime"><?php the_date(); ?></p>
            <h2><?php the_title(); ?></h2>
            <?php the_content(); ?>

            <h4>What's Inside:</h4>
            <ul class="preview olLoop">
            <?php query_posts( array ( 'category_name' => 'Magazine Preview', 'posts_per_page' => 3 ) ); ?>
            <?php while (have_posts()) : the_post(); ?>
            <li>
                <a href="<?php $featuredImage = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );  echo $featuredImage;?>" rel="lightbox" title="">
                Photo Preview
                <br>
                <strong><?php the_title(); ?></strong>
                </a>
            </li>
            <?php endwhile; ?>
            </ul>

            <h3>Available Now.<br>
            For Free.</h3>

            <a href="<?php echo get_post_meta( get_the_ID(), 'magazine_url', true ); ?>" class="button read-it" target="_blank">Read It <i class="icon-chevron-sign-right"></i></a>
            <?php echo get_post_meta($post->ID, 'magazine_url', true); ?>
        </div>

</div><!--/breaking-->

<?php endwhile; ?>
share|improve this question
    
Exactly what goes wrong? –  s_ha_dum Aug 30 '13 at 2:16
    
The last a tag, where it tries to get the custom field "magazine_url" doesnt work. When I get rid of the query_posts inside of the entire WP_query, it works. –  justinedelson Aug 30 '13 at 2:54

1 Answer 1

Your problem, first, is query_posts. Please don't use query_posts. Just don't. Create a new query just like you did when you created $loop.

Second, your inner loop alters the global variable $post. You need to reset that with wp_reset_postdata() after that inner loop runs. I believe that will take care of it.

share|improve this answer

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.