0

I have registered a CPT and created archive-myCPT.php, here I want to exclude a specific post from the main loop.

This is my code:

<?php 
    $query = new WP_Query( array(
   'post_type' => 'cpt',
   'orderby' => 'date', 
   'order' => 'DESC', 
   'posts__not_in' => array( 118 ),
   'posts_per_page' => 1
) );
        if( $query->have_posts() ){ 
            // normal blog layout
                $x = 1;
                while ( $query->have_posts() ){
                    $query->the_post(); 
                    if ( 0 === (int) $post->post_parent ) {
                    get_template_part( 'inc/post-format/content-debate');
                }
  ........................

All the other arguments are working as expected but the 'posts__not_in' => array( 118 ), is not working, I still see it.

What am I doing wrong ?

Thanks !

UPDATE:

This more relevant code which I'm using for the loop:

<?php 

        if( have_posts() ){ 
            // normal blog layout
                $x = 1;
                while ( have_posts() ){
                    the_post(); 
                    if ($post->ID != 118) {
                    get_template_part( 'inc/post-format/content-debate');
                }


                    // advertising between posts
                    if($ad_posts_mode != 'none'){
                        // take into account ad frequency
                        if (($x % $ad_posts_frequency) == 0){

                            switch ($ad_posts_mode) {
                                case 'image':
                                    echo '<div class="'.(($ad_posts_box) ? 'box' : '').' between_posts"><a target="_blank" href="'.of_get_option('ad_posts_image_link').'"><img src="'.of_get_option('ad_posts_image').'"></a></div>';
                                break;
                                case 'html':
                                    echo '<div class="'.(($ad_posts_box) ? 'box' : '').' between_posts">'.apply_filters('shortcode_filter',do_shortcode(of_get_option('ad_posts_code'))).'</div>';
                                break;
                            }
                        }
                    }
                    $x++;
                }
            }
2
  • Your code examples are inconsistent with each other. Please narrow it down to what you actually have right now and all of it together - how are you modifying loop and how do you output results.
    – Rarst
    Jan 15 '14 at 18:41
  • Is 118 a sticky post, by any chance?
    – vancoder
    Jan 15 '14 at 21:32
1

You're just off by one letter it seems.

post__not_in => array(118)

Post & Page Parameters

You can also try adding an if statement, such as:

if($post->ID != 118) :

... run rest of code

Though not optimal, without seeing the rest of your template I can't be certain.

Additional Edit:

<?php
$loop = new WP_Query(array(
    'post_type'       => 'cpt',
    'orderby'         => 'date',
    'order'           => 'DESC',
    'posts_per_page'  => -1
));
if($loop->have_posts()) :while($loop->have_posts()) : $loop->the_post();
    if($post->ID != 118) :
?>
The Title is: <?php the_title(); ?>. The ID is: <?php $post->ID; ?>
<?php endif; endwhile; endif; wp_reset_query(); ?>

Using this simple example on a test CPT I created, and creating a few test posts (plus using a random ID from one of them), the post did not display. If it still displays for you, it is either not the correct $post->ID, or you're missing something from your examples.

4
  • Tried it, but still not excluded...
    – agis
    Jan 15 '14 at 14:24
  • 1
    Either way, that is the correct parameter, not what you have. I have added an additional comment to the original solution.
    – Gilipe
    Jan 15 '14 at 14:26
  • I've updated my question with more relevant code which I'm using for the loop and I've added also if($post->ID != 118) still not excluded.
    – agis
    Jan 15 '14 at 14:46
  • I am going to modify your first initial bit of code, and hopefully you can run it for me. It'll be added onto my previous answer.
    – Gilipe
    Jan 15 '14 at 19:13

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.