WordPress.org

Ready to get started?Download WordPress

Forums

Find Oldest Published Post (4 posts)

  1. Edward Caissie
    Theme Review Admin
    Posted 4 years ago #

    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 4 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. Edward Caissie
    Theme Review Admin
    Posted 4 years ago #

    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. Edward Caissie
    Theme Review Admin
    Posted 4 years ago #

    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

Tags

No tags yet.