Hi I am attempting to use two loops in my WP template as I am using advanced custom fields to display info from a custom post type (college)

I have tested my code (label 'loop which doesn't work' below) at the top of the template file and it pulls the correct data, however when I place it inside the template where I want it, it pulls data from the post it is on.

I suspect this problem might be to do with the function 'your_custom_loop' and needing to set up another function like this for my new loop. Help much appreciated thank you

remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'your_custom_loop' );

function your_custom_loop() { ?>

<article itemtype="http://schema.org/CreativeWork" itemscope="itemscope" class="post-<?php print $pageid; ?> page type-page status-publish entry">
<div class="entry-content" itemprop="text">

do_action( 'genesis_entry_header' );

// Loop which doesn't work: 

$post_object = get_field('resource_college');

if( $post_object ): 

    // override $post
    $post = $post_object;
    setup_postdata( $post ); 

        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <span>Post Object Custom Field: <?php the_field('field_name'); ?></span>
    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>

// Regular loop

if(have_posts()) : while(have_posts()) : the_post();

I think I have seen this problem before and you need to set global $post; on the line before to be able to use setup_postdata($post); as it does not return the $post object, but rather sets it in the global scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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