1

I have created a custom post type named rooms. I would like to get all rooms posts except for the one with the post id 164, and display it using the code below:

<div class="room-container container room-grid">
    <div class="heading-box">
        <h2>rooms</h2>
    </div>

  <?php 
    $my_query   = new WP_Query('post_type=rooms&posts_per_page=-1'); 
    $c          = 1;

    while( $my_query->have_posts() ) : 
        $my_query->the_post();
        $thumb   = get_post_thumbnail_id(); 
        $img_url = wp_get_attachment_url( $thumb );
        $content = get_the_content();
  ?>

    <div class="room-box col-xs-6">
        <div class="img-container">
            <img src="<?php echo $img_url; ?>" alt="rooms">
            <a href="#" class="btn btn-default">More Details</a>
        </div>

        <div class="details">
            <div class="title">
                <a href="#"><?php echo get_the_title(); ?></a>
            </div>

            <div class="desc">
                <?php echo substr( $content, 0, 200 ); ?>...
            </div>  
        </div>
    </div>

  <?php $c++; endwhile; ?>

</div>

3 Answers 3

2

Use the post__not_in Argument of wp_query like this

$query= new WP_Query(array('post_type' => 'rooms', 'post__not_in' => array(164));
1

WP_Query has a ton of indices you can define. The one you need is posts__not_in under Post and Page Parameters section. It would look like this in practice:

$my_query = new WP_Query( array(
    'post_type'      => 'rooms',
    'posts_per_page' => -1,
    'posts__not_in'  => array( 164 ),
) ); 

This will get all posts in the rooms post type with the exception of post_id 164.

0

I got the working code. rooms doesnt need 'rooms'. Thankyou friends..

<?php
    $my_query = new WP_Query( array('post_type' => rooms, 'post__not_in' => array(164)) );
    while ($my_query->have_posts()) : $my_query->the_post(); 
    $thumb = get_post_thumbnail_id(); 
    $img_url = wp_get_attachment_url( $thumb);
    $content = get_the_content();
    ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.