WordPress.org

Ready to get started?Download WordPress

Forums

No duplicate posts with 2 queries (4 posts)

  1. gmacthird
    Member
    Posted 1 year ago #

    The issue that I'm running into is that the sticky post duplicates itself in the second query and populates the first cell of the table. How do I prevent the post from showing up twice?

    I'm running a query for all posts in the category 'tanning' and displaying the singular sticky post for the category 'tanning' with the layout in the 'firsttext' div and 'firstimage' div, successfully gaining the layout that I'm looking for.

    Below that, I'm running a second query for all posts (6 max) that will populate a table, three columns wide, housed in the 'threeposts' div, successfully gaining the layout that I'm looking for.

    I need this to work with the code below as I don't want to mess up the formatting. Any help is appreciated!

    <div id="firsttext">
    <?php
        if ( get_query_var('paged') )
                $paged = get_query_var('paged');
            elseif ( get_query_var('page') )
                $paged = get_query_var('page');
            else
                    $paged = 1;
                    query_posts($ids =  array ( 'category_name' => 'tanning', 'posts_per_page' => 1 ) );
    ?>
    <?php if (have_posts()) : ?>
    		<?php while (have_posts()) : the_post(); ?>
                   <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                   <h3 class="post-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Permanent Link to %s', 'responsive'), the_title_attribute('echo=0')); ?>"><?php the_title(); ?></a></h3>
    
                    <div class="post-meta">
                    <?php responsive_post_meta_data(); ?>
                   	<?php the_tags(__('Tagged with:', 'responsive') . ' ', ', ', '<br />'); ?>
    					<?php printf(__('%s', 'responsive'), get_the_category_list(', ')); ?>
                    </div><!-- end of .post-meta -->
    
                    <div class="post-entry">
                        <?php the_content(__('...read more.', 'responsive')); ?>
                        <?php wp_link_pages(array('before' => '<div class="pagination">' . __('Pages:', 'responsive'), 'after' => '</div>')); ?>
                    </div><!-- end of .post-entry -->
    
                <div class="post-edit"><?php edit_post_link(__('Edit', 'responsive')); ?></div>
                </div><!-- end of #post-<?php the_ID(); ?> -->
    
           <?php endwhile; ?>
     </div>
     <div id="firstimage">
     		<?php if ( has_post_thumbnail()) : ?>
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
                        <?php the_post_thumbnail(); ?>
                            </a>
                        <?php endif; ?>
     </div>
     </div>
    
     <?php
        if ( get_query_var('paged') )
                $paged = get_query_var('paged');
            elseif ( get_query_var('page') )
                $paged = get_query_var('page');
            else
                    $paged = 1;
                    query_posts( array ( 'category_name' => 'tanning', 'posts_per_page' => 6 ) );
    ?> 
    
    </div>
    
    <div id="threepost">
    <table>
    <?php
    $col = 0;
    $cols_per_row = 2;
    while (have_posts()) : the_post();
    if($col == 0) echo '<tr>';
    ?>
    <td class="column'<?php echo $col; ?>" style="vertical-align: top;">
    	<div class="post" id="post-'<?php the_ID();  ?>'">
    	<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail('medium'); ?></a>
        <h3><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    	</div>
    
        <div class="post-meta">
        <?php responsive_post_meta_data(); ?>
        <?php the_tags(__('Tagged with:', 'responsive') . ' ', ', ', '<br />'); ?>
    	<?php printf(__('%s', 'responsive'), get_the_category_list(', ')); ?>
        </div><!-- end of .post-meta -->
    
        <div class="entry"><?php the_content('...read more.'); ?>
        </div>
    
    	</td>
        <?php if($col++ >= $cols_per_row){
    	$col = 0;
    	echo '</tr>';
      } endwhile;
      ?>
    </table>  
    
    </div>
  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    http://codex.wordpress.org/Sticky_Posts#Display_Sticky_Posts

    second query:

    query_posts( array ( 'category_name' => 'tanning', 'posts_per_page' => 6, 'post__not_in' => get_option( 'sticky_posts' ) ) );

    or use the 'ignore_sticky_posts' parameter; http://codex.wordpress.org/Class_Reference/WP_Query#Sticky_Post_Parameters

  3. gmacthird
    Member
    Posted 1 year ago #

    Thank you for the quick reply. Unfortunately that did not work. Is there something in the table that is messing with the query? Or is the formatting of the two queries contradicting one another? I can send you a private message if you'd like to see the url output. Please let me know.

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    there is also the other 'trick' to collect the post IDs from the frist loop and exclude them in the second loop:

    <div id="firsttext">
    <?php
        if ( get_query_var('paged') )
                $paged = get_query_var('paged');
            elseif ( get_query_var('page') )
                $paged = get_query_var('page');
            else
                    $paged = 1;
                    query_posts($ids =  array ( 'category_name' => 'tanning', 'posts_per_page' => 1 ) );
    ?>
    <?php if (have_posts()) : $exclude_ids = array(); ?>
    		<?php while (have_posts()) : the_post(); $exclude_ids[] = $post->ID; ?>
                   <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                   <h3 class="post-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Permanent Link to %s', 'responsive'), the_title_attribute('echo=0')); ?>"><?php the_title(); ?></a></h3>
    
                    <div class="post-meta">
                    <?php responsive_post_meta_data(); ?>
                   	<?php the_tags(__('Tagged with:', 'responsive') . ' ', ', ', '<br />'); ?>
    					<?php printf(__('%s', 'responsive'), get_the_category_list(', ')); ?>
                    </div><!-- end of .post-meta -->
    
                    <div class="post-entry">
                        <?php the_content(__('...read more.', 'responsive')); ?>
                        <?php wp_link_pages(array('before' => '<div class="pagination">' . __('Pages:', 'responsive'), 'after' => '</div>')); ?>
                    </div><!-- end of .post-entry -->
    
                <div class="post-edit"><?php edit_post_link(__('Edit', 'responsive')); ?></div>
                </div><!-- end of #post-<?php the_ID(); ?> -->
    
           <?php endwhile; ?>
     </div>
     <div id="firstimage">
     		<?php if ( has_post_thumbnail()) : ?>
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
                        <?php the_post_thumbnail(); ?>
                            </a>
                        <?php endif; ?>
     </div>
     </div>
    
     <?php
        if ( get_query_var('paged') )
                $paged = get_query_var('paged');
            elseif ( get_query_var('page') )
                $paged = get_query_var('page');
            else
                    $paged = 1;
                    query_posts( array ( 'category_name' => 'tanning', 'posts_per_page' => 6, 'post__not_in' => $exclude_ids ) );
    ?> 
    
    </div>
    
    <div id="threepost">
    <table>
    <?php
    $col = 0;
    $cols_per_row = 2;
    while (have_posts()) : the_post();
    if($col == 0) echo '<tr>';
    ?>
    <td class="column'<?php echo $col; ?>" style="vertical-align: top;">
    	<div class="post" id="post-'<?php the_ID();  ?>'">
    	<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail('medium'); ?></a>
        <h3><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    	</div>
    
        <div class="post-meta">
        <?php responsive_post_meta_data(); ?>
        <?php the_tags(__('Tagged with:', 'responsive') . ' ', ', ', '<br />'); ?>
    	<?php printf(__('%s', 'responsive'), get_the_category_list(', ')); ?>
        </div><!-- end of .post-meta -->
    
        <div class="entry"><?php the_content('...read more.'); ?>
        </div>
    
    	</td>
        <?php if($col++ >= $cols_per_row){
    	$col = 0;
    	echo '</tr>';
      } endwhile;
      ?>
    </table>  
    
    </div>

Topic Closed

This topic has been closed to new replies.

About this Topic