Home > Net >  Problem showing post for the term of the current page Wordpress
Problem showing post for the term of the current page Wordpress

Time:07-15

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 ><span >Luxury Properties for sale in</span> <?php echo $term->name; ?></h2>

        <div >
            <div >

    <?php while ($query->have_posts()) : $query->the_post(); ?>
                    
                <div >
                    <div ><?php the_favorites_button($post_id, $site_id); ?></div>
                    <div >
                        <div >
                            <?php
                            $images = get_field('listing_gallery');

                            if ($images) : ?>
                                <?php foreach ($images as $image) : ?>
                                    <div  style="background-image: url(<?php echo $image['url']; ?>)"></div>
                                <?php endforeach; ?>
                            <?php endif;
                            ?>
                        </div>
                        <div ></div>
                    </div>
                    
                    <div >
                        <div id="lp-region-sale" >
                            <span><?php the_field('property_price'); ?> €</span>
                        </div>
                        <div>
                            <a href="<?php the_permalink(); ?>" ><?php the_title(); ?></a>
                        </div>
                        <div >
                            <div ><span></span><?php the_field('number_of_bedrooms'); ?> Beds</div>
                            <div ><span></span><?php the_field('number_of_bathrooms'); ?> Baths</div>
                            <div ><span></span><?php the_field('interior_square_meters'); ?> Int Sqm</div>
                            <div ><span></span><?php the_field('lot_square_meters'); ?> Lot Sqm</div>
                        </div>
                    </div>
                </div>
                
<?php endwhile;
} ?>


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

?>

CodePudding user response:

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 ><span >Luxury Properties for sale in</span> <?php echo $term->name; ?></h2>

        <div >
            <div >

    <?php while ($query->have_posts()) : $query->the_post(); ?>
                    
                <div >
                    <div ><?php the_favorites_button($post_id, $site_id); ?></div>
                    <div >
                        <div >
                            <?php
                            $images = get_field('listing_gallery');

                            if ($images) : ?>
                                <?php foreach ($images as $image) : ?>
                                    <div  style="background-image: url(<?php echo $image['url']; ?>)"></div>
                                <?php endforeach; ?>
                            <?php endif;
                            ?>
                        </div>
                        <div ></div>
                    </div>
                    
                    <div >
                        <div id="lp-region-sale" >
                            <span><?php the_field('property_price'); ?> €</span>
                        </div>
                        <div>
                            <a href="<?php the_permalink(); ?>" ><?php the_title(); ?></a>
                        </div>
                        <div >
                            <div ><span></span><?php the_field('number_of_bedrooms'); ?> Beds</div>
                            <div ><span></span><?php the_field('number_of_bathrooms'); ?> Baths</div>
                            <div ><span></span><?php the_field('interior_square_meters'); ?> Int Sqm</div>
                            <div ><span></span><?php the_field('lot_square_meters'); ?> Lot Sqm</div>
                        </div>
                    </div>
                </div>
                
<?php endwhile;
} ?>


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

?>

CodePudding user response:

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 ); ?>
  • Related