Is there a way to extract a single post better than using get_posts()?

Question

I have code like the following to extract data about a post:

$args = array(
    'post_type'     => 'products',  
    'numberposts'   => - 1,
    'orderby'       => 'meta_value',
    'meta_key'      => 'release_date',
    'order'         => 'DESC',
    'meta_query'    => array(
        array(
            'key'       => 'status',
            'value'     => 'production',
        )
    )
);

$latest_products = get_posts($args);

echo 'Our latest product is ' . $latest_products[0]->post_title;

Since I’m only interested in the very latest product the release_date custom field in descending order puts the post I want at the top of the array, so its index will always be equal to 0.

However, I am worried that retrieving multiple posts to get data about only one of them is inefficient and may not scale well.

I tried using get_post() as opposed to the current get_posts() but that doesn’t work, perhaps understandably.

Is there a more efficient way to extract data for only one post, not knowing its ID in advance?

0
Dave White 2 years 2020-01-11T08:38:42-05:00 0 Answers 78 views 0

Leave an answer

Browse
Browse