Include a specific post to the query_posts and remove it if it is already in the returned list

Bare with me. Firstly, here is my whole script for my featured slider articles:

<div id="featured" class="<?php if ( $responsive ) echo 'flexslider' . $featured_auto_class; else echo 'et_cycle'; ?>">
<a id="left-arrow" href="#"><?php esc_html_e('Previous','Aggregate'); ?></a>
<a id="right-arrow" href="#"><?php esc_html_e('Next','Aggregate'); ?></a>

<?php if ( $responsive ) { ?>
    <ul class="slides">
<?php } else { ?>
    <div id="slides">
<?php } ?>
<?php global $ids;
$ids = array();
$arr = array();
$i=0;

$featured_cat = get_option('aggregate_feat_cat');
$featured_num = (int) get_option('aggregate_featured_num');

if (get_option('aggregate_use_pages') == 'false') query_posts("showposts=$featured_num&top=billboard"); // set which slug in custom taxonomy TOP it should filter
else {
    global $pages_number;

    if (get_option('aggregate_feat_pages') <> '') $featured_num = count(get_option('aggregate_feat_pages'));
    else $featured_num = $pages_number;

    query_posts(array
    ('post_type' => 'page',
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post__in' => (array) get_option('aggregate_feat_pages'),
        'showposts' => (int) $featured_num
    ));
} ?>
<?php if (have_posts()) : while (have_posts()) :
    global $post;
    if (!$first_time) // START custom first post
    {
        $post_id = 105; // This is the ID of the first post to be displayed on slider
        $post = get_post($post_id);
        $first_time = 1;
    }
    else the_post(); // END custom first post
    ?>
    <?php if ( $responsive ) { ?>
            <li class="slide">
        <?php } else { ?>
            <div class="slide">
        <?php } ?>
    <?php
    $width = $responsive ? 960 : 958;
    $height = 340;
    $small_width = 95;
    $small_height = 54;
    $titletext = get_the_title();

    $thumbnail = get_thumbnail($width,$height,'',$titletext,$titletext,false,'Featured');

    $arr[$i]['thumbnail'] = get_thumbnail($small_width,$small_height,'',$titletext,$titletext,false,'Small');
    $arr[$i]['titletext'] = $titletext;

    $thumb = $thumbnail["thumb"];
    print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, ''); ?>
    <div class="featured-top-shadow"></div>
    <div class="featured-bottom-shadow feat<?php $category = get_the_category(); echo $category[0]->category_nicename; ?>"></div>
    <div class="featured-description">
        <div class="feat_desc">
            <h2 class="featured-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <p><?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,20); ?></p>
        </div>
    </div> <!-- end .description -->
    <?php if ( $responsive ) { ?>
            </li> <!-- end .slide -->
    <?php } else { ?>
            </div> <!-- end .slide -->
    <?php } ?>
    <?php $ids[] = $post->ID; $i++; endwhile; endif; wp_reset_query(); ?>
<?php if ( $responsive ) { ?>
    </ul> <!-- end .slides -->
<?php } else { ?>
</div> <!-- end #slides -->
<?php } ?>
</div> <!-- end #featured -->

It query_posts a set amount of posts from the custom taxonomy top=billboard. The following part of the code:

Read More
<?php if (have_posts()) : while (have_posts()) :
    global $post;
    if (!$first_time) // START custom first post
    {
        $post_id = 105; // This is the ID of the first post to be displayed on slider
        $post = get_post($post_id);
        $first_time = 1;
    }
    else the_post(); // END custom first post
    ?>

Adds the post ID 105 to the first post of the returned list. This allows me to control which post is displayed first. The problem is: If post ID 105 is already included in the custom taxonomy top=billboard, then it will appear twice in the list… so once because it was added in the above custom first post script, and once because it was added in the custom taxonomy top=billboard.

How do I adjust my code so that if post ID 105 exists in custom taxonomy top=billboard, then it should not appear twice (only as the first post).

Related posts

Leave a Reply

2 comments

  1. Okay so I tried to read your code, and failed, and reformatted it as it’s currently unreadable, and any errors are obscured by this unreadability. I cannot stress the importance of indenting correctly and following a standardised formatting for making your life infinitely easier:

    <?php
    $featured_class = 'et_cycle';
    $tag = 'ul';
    $attr = 'class';
    if($responsive){
        $featured_class = 'flexslider' . $featured_auto_class;
        $tag = 'div';
        $attr = 'id';
    }
    
    ?>
    <div id="featured" class="<?php echo $featured_class; ?>">
        <a id="left-arrow" href="#"><?php esc_html_e( 'Previous', 'Aggregate' ); ?></a>
        <a id="right-arrow" href="#"><?php esc_html_e( 'Next', 'Aggregate' ); ?></a>
        <?php
    
    
        echo '<'.$tag.' '.$attr.'="slides">';
    
        global $ids;
        $ids = array();
        $arr = array();
        $i=0;
    
        $featured_cat = get_option( 'aggregate_feat_cat' );
        $featured_num = (int) get_option( 'aggregate_featured_num' );
    
        $query_args = array();
        if ( get_option( 'aggregate_use_pages' ) == 'false' ){
            // set which slug in custom taxonomy TOP it should filter
            $query_args = array(
                'showposts' => $featured_num,
                'top' => 'billboard'
            );
        }else {
            global $pages_number;
    
            if ( get_option( 'aggregate_feat_pages' ) <> '' ){
                $featured_num = count( get_option( 'aggregate_feat_pages' ) );
            } else{
                $featured_num = $pages_number;
            }
            query_args = array(
                    'post_type' => 'page',
                    'orderby' => 'menu_order',
                    'order' => 'ASC',
                    'post__in' => (array) get_option( 'aggregate_feat_pages' ),
                    'showposts' => (int) $featured_num
            );
        }
    
        // needs to die, use WP_Query instead
        query_posts($query_args);
    
    
        if ( have_posts() ) {
            while ( have_posts() ) {
                global $post;
                // START custom first post
                if ( !$first_time ) {
                    $post_id = 105; // This is the ID of the first post to be displayed on slider
                    $post = get_post( $post_id );
                    $first_time = 1;
                }else{
                    the_post(); // END custom first post
                }
                if ( $responsive ) {
                    echo '<li';
                }else {
                    echo '<div';
                }
                echo ' class="slide">';
                $width = $responsive ? 960 : 958;
                $height = 340;
                $small_width = 95;
                $small_height = 54;
                $titletext = get_the_title();
    
                $thumbnail = get_thumbnail( $width, $height, '', $titletext, $titletext, false, 'Featured' );
    
                $arr[$i]['thumbnail'] = get_thumbnail( $small_width, $small_height, '', $titletext, $titletext, false, 'Small' );
                $arr[$i]['titletext'] = $titletext;
    
                $thumb = $thumbnail["thumb"];
                print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, '' );
                ?>
                <div class="featured-top-shadow"></div>
                <div class="featured-bottom-shadow feat<?php $category = get_the_category(); echo $category[0]->category_nicename; ?>"></div>
                <div class="featured-description">
                    <div class="feat_desc">
                        <h2 class="featured-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                        <p><?php $excerpt = get_the_excerpt(); echo string_limit_words( $excerpt, 20 ); ?></p>
                    </div>
                </div> <!-- end .description -->
                    <?php if ( $responsive ) { ?>
                            </li> <!-- end .slide -->
                    <?php } else { ?>
                            </div> <!-- end .slide -->
                    <?php } ?>
                    <?php $ids[] = $post->ID; $i++;
            }
        }
    
        wp_reset_query();
        echo '</'.$tag.'>';
        ?>
    </div> <!-- end #featured -->
    

    You’ll notice i made some simplifications and swapped out the if statements so they all use {} rather than a muddle of shorthand and standard.

    There are a number of issues here, the one at the end being wp_reset_query, and the biggest alarm bell being the use of query_posts.

    I strongly recommend you never use query_posts and instead use either WP_Query or get_posts.

    The culprit here is your loop:

            while ( have_posts() ) {
                global $post;
                // START custom first post
                if ( !$first_time ) {
                    $post_id = 105; // This is the ID of the first post to be displayed on slider
                    $post = get_post( $post_id );
                    $first_time = 1;
                }else{
                    the_post(); // END custom first post
                }
    

    Here we see the beginning of your post loop, it checks if there are additional posts, then calls the_post to advance forwards.

    When the first post happens however, you don’t call the_post, so the loop fails to advance. This is not how a loop should work, you should always call the_post regardless.

    Instead, I’d suggest you specify in your query not to include the post 105. ( I would also avoid hardcoding the post ID, it may not always be a 105 ).

    So, to start with, use the post__not_in parameter on your query, e.g.:

    $query_args['post__not_in'] = array(105);
    
  2. i use pre_get_posts. example:

    add_action('pre_get_posts','search_filterr');function search_filterr($query) {
        if ( !is_admin() && $query->is_main_query() )   {
            if ( $query->is_search ) { //$query->is_category or etc...except is_page()
                $arrs[]='post';
                $arrs[]='page';
                $arrs[]='my_carss';
                $query->set('post_type',  $arrs );
            }
        }
    }