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.

This is going to sound a little nuts. I have a situation where I'm displaying a list of products that a user selects to move to the products' page. I also have documents and videos that I created custom post types and fields for. I need to run a second loop within the current loop to pull a custom field from a custom post type. If that makes sense?

for example this is the product "directory" loop (pardon my hackish techniques)

<div id="content" role="main">
<?php 
    $parent = $post->ID; 
    query_posts('post_type=page&order=ASC&orderby=title&post_parent='.$parent);?>
<?php while (have_posts()) : the_post(); ?>
<?php $model = get_the_title(); ?>
<div class="product-selection-container round">
    <h2 class="directory-title"><a href="<?php the_permalink() ?>"><?php echo get_post_meta($post->ID, "product-main-title", true); ?></a></h2>
    <h3>Model <?php echo $model ?></h3><!-- the model -->
    <?php echo my_excerpts(); ?>
    <ul class="round">
        <li><a href="<?php the_permalink() ?>">Learn More</a></li>
        <li><a href="">Specification Sheet</a></li>
        <li><a href="">Video Tour</a></li>
    </ul>
</div>
</div>
<?php endwhile; ?>

And this is the loop I generally use to add documents to the pages (excluding the custom loop template)

<?php
        $documents = array('numberposts' => 5, 'post_type' => 'documents', 'category_name' => $model);
        query_posts( $documents );
        get_template_part( 'loop', 'documents' );
        wp_reset_query();
 ?>

I tried combining the two where I would add the second loop to <li><a href="">Specification Sheet</a></li> but that caused the page to run in a perpetual loop.

An alternative option I was looking for was to pull a single custom field from a post type without creating a loop. For example.

get post_type => document, meta_key => document-type, meta_value => spec-sheet

Any help would be appreciated.

share|improve this question
    
Have a look at the loop docs on the codex, for nested loops or multiple loops on same page don't use query_posts, use something like $my_query = new WP_Query . –  Wyck Mar 25 '11 at 18:14
    
Yeah I've been having a tough time converting my current loops o get_posts or WP_Query. –  Zach Shallbetter Mar 25 '11 at 18:19

1 Answer 1

up vote 2 down vote accepted

I'll presume you are using something like this...to get the custom value you want. Once you are inside the loop.

$customfieldvalue = get_post_meta($post->ID, "metakeyname", true);

Since you are using $model as the base for a query. You could create a function That will query what you want, and then reset the current query. You could use it wherever you want. Here is a template.

function modelquery($model) {
    global $wp_query, $post, $paged, $post_count;

        // YOUR QUERY
        $query_args = array (
            'numberposts' => 5, 
            'post_type' => 'documents', 
            'category_name' => $model
        );    

        // SAVE CURRENT QUERY
        $temp = $wp_query;
        $wp_query= null;    

        // CREATE NEW QUERY
        $wp_query = new WP_Query();
        $wp_query->query($query_args);      

        // THE LOOP, DO WHAT YOU HAVE TO DO HERE
        while ($wp_query->have_posts()) : $wp_query->the_post();    
            $customfieldvalue = get_post_meta($post->ID, "metakeyname", true);
            echo $customfieldvalue;
        endwhile;   

        // SWAP BACK THE PREVIOUS QUERY
        $wp_query = null; 
        $wp_query = $temp;
        wp_reset_query();       


}
share|improve this answer
    
Thanks Wok, I'll try this out shortly. –  Zach Shallbetter Mar 31 '11 at 17:58

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.