Fernando Souza
Fernando Souza

Reputation: 1779

Display only the latest post for each category in Wordpress

I know how to display the latest posts of my website, but I wonder if there is way to make a list of the latest posts, displaying only one post per category. Lets say we have 4 categories, so only 4 posts will be displayed on the list, ordered according to its release date. What should I do? Here is my present code:

<?php $postid = get_the_ID(); ?>
                    <?php
                        $args = array(
                            'post_type' => 'topics',
                        );
                    ?>
                    <?php $query = new WP_Query( $args ); ?>
                    <?php if($query -> have_posts()): ?>
                <?php while($query -> have_posts()): $query->the_post();?>

                        <article class="article">
                            <a href="<?php the_permalink(); ?>">
                                <?php
                                if ($terms = get_the_terms($post->ID, 'topics_category')) :
                                foreach ( $terms as $term ) :
                                $term_slug = $term -> slug;
                                $term_link = get_term_link( $term );
                                ?>
                                <p class="article__category">
                                    <?php
                                        echo $term->name;
                                    ?>
                                </p>
                                <?php
                                endforeach;
                                endif;
                                ?>
                                <h2 class="article__title"><?php the_title(); ?></h2>
                                <p class="article__date"><?php the_time('Y年m月d日'); ?></p>
                            </a>
                        </article>

                <?php endwhile; ?>
                <?php endif; ?>
                <?php wp_reset_postdata();  ?>

Upvotes: 0

Views: 2399

Answers (2)

bhavesh vala
bhavesh vala

Reputation: 873

You can replace your code

    <?php $postid = get_the_ID(); ?>
    <?php
        $args = array(
            'post_type' => 'topics',
            'post_status' => 'publish'
        );
    ?>
    <?php $query = new WP_Query( $args ); ?>
    <?php if($query -> have_posts()): ?>
<?php while($query -> have_posts()): $query->the_post();?>
        <article class="article">
            <a href="<?php the_permalink(); ?>">
                <?php
                if ($product_terms = wp_get_object_terms($post->ID, 'topics_category')) :                    
                    foreach ( $product_terms as $term ) :
                    $term_slug = $term->slug;
                    $term_link = get_term_link( $term, 'topics_category' );
                    ?>
                    <p class="article__category">
                        <?php
                            echo $term->name;
                        ?>
                    </p>
                    <?php
                    endforeach;

                endif;
                ?>
                <h2 class="article__title"><?php the_title(); ?></h2>
                <p class="article__date"><?php the_time('Y年m月d日'); ?></p>
            </a>
        </article>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata();  ?>

If it's not working then you replace function wp_get_object_terms to wp_get_post_terms

For knowledge

https://codex.wordpress.org/Function_Reference/wp_get_post_terms

and

https://codex.wordpress.org/Function_Reference/wp_get_object_terms

Upvotes: 2

Raf
Raf

Reputation: 352

You should make this the other way around, namely first get the categories using get_terms() and then in foreach loop use WP_Query to get one post for each category. In WP_Query args you should pass the posts_per_page => 1 param and also tax_query parameter. For available parameters have a look here: https://gist.github.com/luetkemj/2023628

Hope it helps

Upvotes: 1

Related Questions