Find Oldest Published Post (4 posts)

  1. I have written a function that uses the single oldest published post, but it has been suggested it may be improved upon.

    Currently I am identifying the oldest post with this snippet:

    /* Get all posts */
    $all_posts = get_posts('post_status=publish&order=ASC');
    /* Get first post */
    $first_post = $all_posts[0];

    As this function is being used on every "public-facing page" is there a more efficient or less server intense method than the one I am using?

  2. esmi
    Forum Moderator
    Posted 6 years ago #

    As far as I can tell, that should only involve a single db query. Can't see how you could reduce that.

  3. Thanks, I was just wondering if there may be another function, or variation, to find the oldest post. The code above was the most concise I could think of.

  4. OK, so this little bit of code has thrown me a bit of a curve ball ...

    The above code finds the first post; short of running the query twice, any ideas on how to include an additional post_type, such as "page" in this query?

    I would like to avoid the following:

    /* Get all posts */
    $all_posts = get_posts('post_status=publish&order=ASC');
    /* Get first post */
    $first_post = $all_posts[0];
    wp_reset_query(); /* required? */
    /* Get all pages */
    $all_pages = get_posts('post_status=publish&order=ASC&post_type=page');
    /* Get first page */
    $first_page = $all_pages[0];

    The result I am looking for is the first published "entry", whether it be a post or a page.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.