skip to Main Content

I’m trying to list the posts related to the current taxonomy page but it shows me all the posts instead. When I output the current term name is correct so really I don’t know why this happens. I’m pasting here the code if anyone can put me in the right direction.

<?php
$term = get_queried_object();

// Define the query
$args1 = array(
    'post_type' => 'properties-for-sale',
    'taxonomy' => $term->term_id
);
$query = new WP_Query($args1);

if ($query->have_posts()) { ?>

<h2 class="region-listing-title"><span class="thin-span">Luxury Properties for sale in</span> <?php echo $term->name; ?></h2>

        <div class="swiper myswiper swiper-h">
            <div class="swiper-wrapper">

    <?php while ($query->have_posts()) : $query->the_post(); ?>
                    
                <div class="swiper-slide">
                    <div class="favorite-button"><?php the_favorites_button($post_id, $site_id); ?></div>
                    <div class="swiper myswiper2 swiper-v">
                        <div class="swiper-wrapper">
                            <?php
                            $images = get_field('listing_gallery');

                            if ($images) : ?>
                                <?php foreach ($images as $image) : ?>
                                    <div class="swiper-slide highlight-img" style="background-image: url(<?php echo $image['url']; ?>)"></div>
                                <?php endforeach; ?>
                            <?php endif;
                            ?>
                        </div>
                        <div class="swiper-pagination"></div>
                    </div>
                    
                    <div class="listing-informations">
                        <div id="lp-region-sale" class="listing-information-price">
                            <span><?php the_field('property_price'); ?> €</span>
                        </div>
                        <div>
                            <a href="<?php the_permalink(); ?>" class="listing-information-title"><?php the_title(); ?></a>
                        </div>
                        <div class="listing-informations__listing-details">
                            <div class="listing-information-label"><span></span><?php the_field('number_of_bedrooms'); ?> Beds</div>
                            <div class="listing-information-label"><span></span><?php the_field('number_of_bathrooms'); ?> Baths</div>
                            <div class="listing-information-label"><span></span><?php the_field('interior_square_meters'); ?> Int Sqm</div>
                            <div class="listing-information-label"><span></span><?php the_field('lot_square_meters'); ?> Lot Sqm</div>
                        </div>
                    </div>
                </div>
                
<?php endwhile;
} ?>


            </div>
        </div>
                    
<?php wp_reset_postdata();

?>

2

Answers


  1. Try with:

    <?php
    $term = get_queried_object()->term_id;
    
    // Define the query
    $args1 = array(
        'post_type' => 'properties-for-sale',
        'taxonomy' => $term
    );
    $query = new WP_Query($args1);
    
    if ($query->have_posts()) { ?>
    
    <h2 class="region-listing-title"><span class="thin-span">Luxury Properties for sale in</span> <?php echo $term->name; ?></h2>
    
            <div class="swiper myswiper swiper-h">
                <div class="swiper-wrapper">
    
        <?php while ($query->have_posts()) : $query->the_post(); ?>
                        
                    <div class="swiper-slide">
                        <div class="favorite-button"><?php the_favorites_button($post_id, $site_id); ?></div>
                        <div class="swiper myswiper2 swiper-v">
                            <div class="swiper-wrapper">
                                <?php
                                $images = get_field('listing_gallery');
    
                                if ($images) : ?>
                                    <?php foreach ($images as $image) : ?>
                                        <div class="swiper-slide highlight-img" style="background-image: url(<?php echo $image['url']; ?>)"></div>
                                    <?php endforeach; ?>
                                <?php endif;
                                ?>
                            </div>
                            <div class="swiper-pagination"></div>
                        </div>
                        
                        <div class="listing-informations">
                            <div id="lp-region-sale" class="listing-information-price">
                                <span><?php the_field('property_price'); ?> €</span>
                            </div>
                            <div>
                                <a href="<?php the_permalink(); ?>" class="listing-information-title"><?php the_title(); ?></a>
                            </div>
                            <div class="listing-informations__listing-details">
                                <div class="listing-information-label"><span></span><?php the_field('number_of_bedrooms'); ?> Beds</div>
                                <div class="listing-information-label"><span></span><?php the_field('number_of_bathrooms'); ?> Baths</div>
                                <div class="listing-information-label"><span></span><?php the_field('interior_square_meters'); ?> Int Sqm</div>
                                <div class="listing-information-label"><span></span><?php the_field('lot_square_meters'); ?> Lot Sqm</div>
                            </div>
                        </div>
                    </div>
                    
    <?php endwhile;
    } ?>
    
    
                </div>
            </div>
                        
    <?php wp_reset_postdata();
    
    ?>
    
    Login or Signup to reply.
  2. Just use tax query.

    <?php
    $term = get_queried_object()->term_id;
    $args = array(
    'post_type' => 'properties-for-sale',
    'tax_query' => array(
        array(
        'taxonomy' => $term->taxonomy,
        'field' => 'term_id',
        'terms' => $term->term_id
         )
      )
    );
    $query = new WP_Query( $args ); ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search