0

i need to display the latest 5 posts of each post type, something like:

Latest BMW Posts:

  • Post title BMW 1
  • Post title BMW 2

Latest Audi Posts:

  • Post title Audi 1
  • Post title Audi 2

And so on.

It seems that https://codex.wordpress.org/Function_Reference/wp_get_recent_posts does it. Something like:

<h2>Latest BMW Posts:</h2>
<ul>
<?php
    $args = array(
    'numberposts' => 5,
    'post_type' => 'bmw'
);
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){
        echo '<li><a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a> </li> ';
    }
    wp_reset_query();
?>
</ul>

<h2>Latest Audi Posts:</h2>
<ul>
<?php
    $args = array(
    'numberposts' => 5,
    'post_type' => 'audi'
);
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){
        echo '<li><a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a> </li> ';
    }
    wp_reset_query();
?>
</ul>

The problem is: when you have 10 or more types it can get overwhelming.

  1. Can the code be used with an array of post types and loop them to avoid repeating the same code for each type?
  2. Is this really a light task or would it kill MySQL? If so what's the most efficient way to get such output?
3
  • Please try to ask only one question at a time. 1. You can place that code inside a function and pass the post_type as an argument. 2. That depends. You'll have to make the queries somehow, it will probably be best to cache the result and invalidate the cached page if any of the post types contents' changes.
    – kero
    Commented Sep 27, 2018 at 18:19
  • 1
    Have you considered using only one post type car and differentiating via a taxonomy? Doesn't really change anything for you for now, but in the future it will be alot easier. Also you can just query for that taxonomy and then get all of their posts
    – kero
    Commented Sep 27, 2018 at 18:20
  • @kero Thanks, great idea! Sorry for sneaking 2 questions! The site already have thousands of posts of each type sadly. Commented Sep 27, 2018 at 18:38

1 Answer 1

3
<?php $post_types = array(
    'bmw' => 'BMW',  // post type to use in query => post type to show in <h2>
    'audi' => 'Audi'

); 

foreach( $post_types as $post_type => $post_type_name ):
?>

    <h2>Latest <?php echo $post_type_name; ?> Posts:</h2>
    <ul>
    <?php
        $args = array(
        'numberposts' => 5,
        'post_type' => $post_type
    );
        $recent_posts = wp_get_recent_posts( $args );
        foreach( $recent_posts as $recent ){
            echo '<li><a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a> </li> ';
        }
        wp_reset_query();
    ?>
    </ul>
<?php endforeach; ?>

I think it's better to use wp_get_recent_posts is better than one single query, because of the filters that may be triggered, because of query cache, because the Db can be scaled into partitions for each post type and because there's no good single query to get all the info faster.

0

Your Answer

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

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