• I know this topic has been addressed elsewhere on the forums, but I can’t seem to get it to work. I’m new to php so the syntax is tricky to me. Basically I have a category page where posts overlap categories, and I want to list them in order of importance. That works fine, the problem is I get duplicating posts. Here’s my code:

    <?php query_posts('category_name=featured');
     if ( have_posts() ) : while ( have_posts() ) : the_post();
     $do_not_duplicate = $post->ID;
     $custom_field_date = get_post_meta($post->ID, 'project-date', true);?>
    
    		<!--do stuff-->
    
    <?php endwhile;
    wp_reset_query();
    ?>		
    
    <?php query_posts('category_name=winners');
     while ( have_posts() ) : the_post();
     $do_not_duplicate = $post->ID;
     $custom_field_date = get_post_meta($post->ID, 'project-date', true);?>
    
    		<!--do stuff-->
    
    <?php endwhile;
    wp_reset_query();
    ?>
    
    <?php query_posts('category_name=portfolio&showposts=50');
     while ( have_posts() ) : the_post();
     $do_not_duplicate = $post->ID;
     $custom_field_date = get_post_meta($post->ID, 'project-date', true);?>
    
    	<!--do stuff-->
    
    <?php endwhile; else: ?>
    
    	<p>Sorry, no posts matched your criteria.</p>
    
    <?php endif; ?>

    What am I missing? Thanks —

Viewing 5 replies - 1 through 5 (of 5 total)
  • How/when are you checking against $do_not_duplicate? And how are you carrying the “already displayed posts” data over into the next query?

    Thread Starter exit6

    (@exit6)

    I guess I’m not, i mean you’re looking at all the code. Like I said, I’m new to php/wordpress. I’m used to Actionscript, and stuff like “$post->ID;” totally baffles me.

    $post->ID is a just a way of referencing the id of the current post.

    The <!--do stuff--> is where you need to add your code for displaying the posts. The code inside The Loop in your index.php file should give you some idea of what to use here.

    Thread Starter exit6

    (@exit6)

    Right, here’s what’s in the <!–do stuff–> area, I left it out because I didn’t think it would help solve the problem, since that’s just about how the given post is displayed. I’m trying to weed out certain posts before they get displayed. Here’s the code:

    <a href="<?php the_permalink(); ?>" class="hp-entry-link">
    <div class="category-entry">
        <p><img class="alignnone" title="<?php the_title(); ?>" src="<?php echo $custom_field_image; ?>" alt="<?php the_title();?>"></p>
        <div class="entry-copy">
            <h4><?php the_title(); ?></h4>
            <p class="client"><?php echo $custom_field_client; ?></p>
            <p class="date"><?php echo $custom_field_date; ?></p>
            <p class="deliverable"><?php echo $custom_field_deliverable; ?></p>
            <p class="disciplines"><?php echo $custom_field_disciplines; ?></p>
        </div><!--end entry-copy-->
    </div></a><!--end category-entry-->

    That works just like I want (there are some custom fields in there that I didn’t pu in the original post). The problem is duplicate posts sneaking through.

    There’s still no check against $do_not_duplicate, though.

    How about this? Just before <?php query_posts('category_name=featured');, add <?php $displayed = array();?>. That will set up an array to store the ids of any posts that have been displayed. Then instead of setting $do_not_duplicate', you check to see if the post ID is already in the $displayed array.

    if(in_array($post->ID,$displayed)) continue; // already displayed this post so go to top of current loop
    else $displayed[] = $post->ID; // about to display - so add it to the array

    Not tested this, so apologies in advance if there are any syntax errors.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Getting $do_not_duplicate to work’ is closed to new replies.