0

I'm having some trouble trying to display current posts by tags and not adding the current posts. Here is my work in progress and I'm using a custom post type. I'm not exactly sure on what to add in order to exclude current post.

<ul>
<?php


    $packages = new WP_Query( array( 'post_type' => 'properties',  'tag' => $tags, 'post__not_in' => array( $post->ID ) ) );

    if( $packages->have_posts() ) { while( $packages->have_posts() ) {
       $packages->the_post();
            ?>

    <li>
        <div class="featured-image">
            <?php if ( has_post_thumbnail()) { $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large'); echo '<img src="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" alt="' . the_title_attribute('echo=0') . '">';  } ?> 
        </div>
        <div class="content">
            <h3><?php the_title(); ?></h3>
            <p><?php the_excerpt(); ?></p>
            <a href="<?php the_permalink(); ?>" >Learn More <i class="fa fa-long-arrow-right"></i></a>
        </div>
    </li>

            <?php
                }
            }
            else {
                echo '<p class="post-notification">No New Articles</p>';
            }
?>
</ul>

UPDATED CODE

<?php

global $post;

    $packages = new WP_Query( array( 'post_type' => 'properties',  'tag' => $post->ID, 'post__not_in' => array( $post->ID ) ) );

    if( $packages->have_posts() ) { while( $packages->have_posts() ) {
       $packages->the_post();
            ?>
  • Try to output $post->ID content ... it could be empty, so your query does not work as expected. Adding global $post; after <?php line could help. – Marek Sep 15 '15 at 3:53
  • still the same, check updated code. I'm doing it on my localhost at the moment – clestcruz Sep 15 '15 at 4:06
  • 'tag' => $post->ID is obviously wrong now, in your query. And did you tried to echo $post->ID? Has it any value? – Marek Sep 15 '15 at 4:19
  • Sorry I'm not really knowledgeable when it comes to php and backend. Yah I tried to echo '$post-ID' and it comes out nothing – clestcruz Sep 15 '15 at 4:30
  • Well, if $post->ID is empty, then post__not_in in your query arguments is ignored, of course. And query results will include current post... where exactly is your code placed? In what theme file? – Marek Sep 15 '15 at 4:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.