1

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?

1 Answer 1

0

I think I found how to do it: setting

   'numberposts'   => 1,

will retrieve only one (the first) post.

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.