Bojana Šekeljić
Bojana Šekeljić

Reputation: 1056

Multiple loop issue - pull 1 featured and continue the loop without it

I am writing a custom multiple loop to be used on a custom category template page. The loop should put one post that is checked as featured in admin, in a separate div, and continue the loop displaying all posts from the category except the featured.

Similar to the example provided on the codex page except I don't want to create a separate category for the featured post.

I am using Advanced Custom Fields plugin for the check box that sets posts as featured.

I have the following issue with my code: if ($post->ID == $do_not_duplicate) continue; prevents rest of the loop to be executed. The code below just pulls the latest featured post.

Here is my function:

function featured() {
$featured = new WP_Query(array(
    'meta_query' => array(
        array(
            'key' => 'featured',
            'value' => '"top"',
            'compare' => 'LIKE'
            )
        ),
    'posts_per_page' => 1
    ));

while ( $featured->have_posts() ) : $featured -> the_post(); 
$do_not_duplicate = $post->ID; ?>
    <div id="featured">
        //featured post
    </div><!-- end #featured -->
<?php 
endwhile;
if(have_posts()) : while (have_posts()) : the_post();
if ($post->ID == $do_not_duplicate) continue;
    ?>
    <div class="container">
    // normal posts
    </div><!-- .charities-container -->
    <?php 
    endwhile;
endif;
}

Your fresh eyes will help a lot!

Thanks!

Upvotes: 0

Views: 165

Answers (1)

mathielo
mathielo

Reputation: 6795

Looks like your $post variable isn't being assigned with the current post information through the loop. As far as I can tell, you could try either one of these ways:

1) Global $post

The $post variable is within your featured() function scope. Therefore, when you run your loop, it is not being recognized as the global $post variable, which is the one WordPress fills with post information through the loop. Simply declare $post as a global variable at the beginning of your function scope and you should be able to gather post information:

function featured() {
    global $post;
    // ... all your function code, $post->ID should work now
}

or 2) Use get_the_ID()

You could replace $post->ID for WP's native function get_the_ID(). This is just about the same as the previous solution, as this function will retrieve the ID property from the global $post object automatically. I would take this as the best solution, as you don't have to worry about scope whenever using post functions (get_the_ID(), get_the_title(), etc) as long as the $post object is populated (after the_post() is called).

So you could replace this line:

$do_not_duplicate = $post->ID;

for

$do_not_duplicate = get_the_ID();

and

if ($post->ID == $do_not_duplicate) continue;

for

if (get_the_ID() == $do_not_duplicate) continue;

Try either one of these solutions, I bet both should work for you. Actually, the example you took from codex page works just fine, the problem is that you applied it inside a local function. This way your $post variable is a local (function scope) variable, and not a global one.

Upvotes: 1

Related Questions